The pair put on 139 for the sixth wicket as the tourists, resuming at 118-5 and still 182 runs behind Sri Lanka's first innings total of 300, went to lunch on a more comfortable 259-6.
Sarfraz, a wicket-keeper who relishes a challenge with the bat, scored 96 off 86 balls in a stand that was studded with 13 hits to the fence.
Sarfraz narrowly missed his fourth Test century when he ambitiously attempted to sweep a ball from seamer Dhammika Prasad that was pitched outside the off-stump and dragged the ball onto his stumps.
Sarfraz and Shafiq had joined hands on Friday evening after the fall of skipper Misbah-ul Haq's wicket that left Pakistan struggling at 96-5.
Also Read
Sarfraz's latest effort followed his impressive batting during the previous series between the two teams in Sri Lanka last year when he scored 55 and 52 in the Galle Test and 103 and 55 in the next match in Colombo.
When Sarfraz reached 47, he had completed 1,000 Test runs in 28 innings to equal Imtiaz Ahmed's record of being the fastest to the mark among Pakistani wicket-keepers.
The three-Test series will be followed by five one-day internationals and two Twenty20 matches.