Pakistan leg-spinner Yasir Shah grabbed the key wicket of Dinesh Chandimal to leave Sri Lanka fighting to avoid defeat after the fourth day of the first Test in Abu Dhabi on Sunday.
The prolific wicket-taker took 2-25 as Sri Lanka struggled to 69-4, needing a rearguard action to save the Test.
Pakistan will be expecting to push for victory in the opening match of a two-Test series on the fifth and final day.
It was tough to negotiate spin on a fourth-day Sheikh Zayed Stadium pitch, but despite a 32nd Test five-wicket haul for veteran Sri Lankan spinner Rangana Herath, Pakistan posted 422 to take a slender three-run lead.
Sri Lanka had made 419 in their first innings.
At the close, Kusal Mendis was batting on 16, with nightwatchman Suranga Lakmal on two, after a day of fluctuating fortunes.
Herath's 5-93 in 40 overs were well foiled by a brilliant 76 by Test debutant Haris Sohail, as initially it looked as though Sri Lanka would take a first-innings lead.
Pakistan then hit back in the second innings through Shah, who first had opener Dimuth Karunaratne caught for 10 and then dismissed first-innings centurion Chandimal, caught at slip, for seven.
Sohail followed his responsible knock by claiming Kaushal Silva's wicket, whom he trapped leg before for 25, while part-timer Asad Shafiq had Lahiru Thirimanne for seven.
Earlier in the day, Pakistan too were struggling at 316-6 and were in danger of conceding a healthy lead with Herath using all his experience and guile.
The 39-year-old left-arm spinner, who now has 394 wickets in 84 Tests, gave his team the much-needed wicket of a resolute Azhar Ali, luring him into an uppish drive which was brilliantly caught at short mid-wicket by a diving Mendis.
Azhar's defiant knock of 85 lasted more than five hours and included four boundaries, but with his dismissal Pakistan's hopes of building a lead looked slim.
But Sohail had other ideas as he added an invaluable 50 for the ninth wicket with tail-ender Hasan Ali to lift Pakistan from 340-8.
Sohail hit seven fours and two sixes before holing out to paceman Nuwan Pradeep, who finished with 2-77. Hasan's whirlwind 25-ball 29 featured three sixes and two fours.
The second and final Test — a day-night affair — will be played in Dubai from October 6.