Galle - Pakistan turned the tables on Sri Lanka in the first Test after Asad Shafiq hit a century to lead a remarkable rearguard action by the lower order on the fourth day on Saturday.
The tourists, who resumed the day at 118-5 in reply to Sri Lanka's 300, piled up 417 in their first innings before being dismissed in the post-tea session with a lead of 117 runs in Galle.
Sri Lanka were 63-2 in their second knock at stumps, having lost first-innings centurion Kaushal Silva (five) and star batsman Kumar Sangakkara (18) in the 21 overs before close.
Azhar Ali caught Silva at third slip off Wahab Riaz and later snapped up Sangakkara at short-leg off Yasir Shah.
Opener Dimuth Karunaratne was unbeaten on 36 and nightwatchman Dilruwan Perera had yet to score as Sri Lanka enter the final day's play on Sunday still trailing by 54 runs with eight wickets in hand.
Pakistan's last five wickets put on 321 runs after the top five had fallen for 96 on Friday as the home team's bowlers struggled on a slow pitch at the Galle International Stadium.
Shafiq was last man dismissed for 131, his seventh Test century and the third in the last four matches following scores of 137 against New Zealand in Sharjah and 107 against Bangladesh in Dhaka.
Sarfraz Ahmed, a wicket-keeper who relishes a challenge with the bat, scored 96 off 86 balls during a sixth-wicket partnership of 139 with Shafiq, his overnight partner.
Sarfraz 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.
Tailender Zulfiqar Babar, essentially a left-arm spinner, smashed a career-best 56 off 60 balls with six boundaries and two sixes as he helped Shafiq add 101 valuable runs for the ninth wicket.
The entertaining partnership ended when Babar was caught in the deep, while Shafiq was stumped soon after to terminate the innings.
Off-spinner Perera finished with four for 122 and seamer Dhammika Prasad took three for 91, but seasoned left-arm spinner Rangana Herath went for one for 99 in 30 overs.
"Gaining the important wicket of Sangakkara at the end has given us a good chance to win the match," said Sarfraz. "Hopefully we will get them out quickly and then chase whatever target we are given."
Sarfraz and Shafiq had joined hands on Friday evening after the fall of the fifth wicket of skipper Misbah-ul Haq had threatened a total collapse.
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 Pakistan's wicket-keepers.
Resuming after lunch at 259-6, Shafiq found good support from Wahab (14) and Yasir (23) before Babar settled in to provide unexpected resistence to the Sri Lankan bowlers.
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.