Sri Lanka spinner Rangana Herath returned career best figures of nine for 127 but Pakistan took a first-innings lead of 12 after wicketkeeper Sarfraz Ahmed scored his maiden test century on the third day of the second test.
Pakistan were all out for 332 at the stroke of lunch, replying to Sri Lanka's first innings total of 320.
The visitors' last four wickets added 88 runs to their overnight score of 244 for six, with Sarfraz (103) moving from 66 to his hundred with a superb pulled six off Chanaka Welegedara to become the first Pakistani wicket-keeper in five years to get to three figures.
Sarfraz held the Pakistan tail together during his 127-ball innings before Herath finally had him edging a catch to wicket-keeper Niroshan Dickwella.
Sarfraz hit one six and seven fours and added 40 runs with Abdur Rehman (16) for the seventh wicket and 28 with Wahab Riaz (17) for the eighth.
Herath continued to run through the Pakistan tail picking up the wickets of Rehman, caught by Mahela Jayawardene at slip, Riaz and Saeed Ajmal (four). Junaid Khan was unbeaten on 13.
Herath's morning spell on Saturday was four for 29 off 8.1 overs. He became only the second Sri Lankan bowler to take nine wickets in an innings after former off-spinner Muttiah Muralitharan. (Reuters)