Asad Shafiq and Sarfraz Ahmed hit battling half-centuries on day four to thwart Sri Lanka and set up an enthralling finish to the second and final Test in Dubai.
Shafiq scored an unbeaten 86 while skipper Sarfraz was 57 not out as they lifted Pakistan from a perilous position at 52-5 to 198-5 at close, after being set a daunting target of 317.
The pair added 146 for the unbroken sixth-wicket stand to leave Pakistan needing another 119 runs with five wickets in hand to level the series after Sri Lanka won a nail-biting first Test in Abu Dhabi by 21 runs.
Shafiq and Ahmed batted confidently to raise hopes of Pakistan maintaining their unbeaten record in nine Test series in the UAE – their adopted home since 2009.
Earlier in the day, paceman Wahab Riaz grabbed 4-41 and left-arm spinner Haris Sohail took three wickets in a single over to dismiss Sri Lanka for 96 in their second innings, after they resumed on 34-5.
However, Pakistan made a disastrous start to their chase when opener Sami Aslam fell for one, caught in the slips off Lahiru Gamage.
Masood and Azhar Ali (17) added 31 for the second wicket before the batting collapse began with Ali caught off fast bowler Nuwan Pradeep for 17.
It was off-spinner Dilruwan Perera who ripped apart Pakistan with a triple strike in the second session. He dismissed Haris Sohail (10), Shan Masood (21) and Babar Azam (nought) to leave Pakistan in tatters at 52-5.