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.
Shafiq was unbeaten at the break on a fluent 64 and Wahab Riaz was on eight, with Pakistan only 41 runs behind with four wickets in hand.
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.
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.
Bad weather had washed out the entire first day's play on Wednesday and just 64 overs were bowled on the second day due to a wet outfield.
The three-Test series will be followed by five one-day internationals and two Twenty20 matches.– AFP
Original source: Ahmed stands firm for Pakistan