Asad Shafiq and Sarfraz Ahmed gave Pakistan hope of drawing the series with an unlikely victory in a captivating second Test after Sri Lanka were embarrassingly dismissed for only 96 on day four in Dubai.
Wahab Riaz took 4-41 and part-time spinner Haris Sohail (3-1) became the first man to take three wickets bowling just one over in the longest format as the tourists were pitifully skittled out on Monday.
Dilruwan Perera (3-76) came to the fore to leave Pakistan in the mire on 52-5 chasing 317 to win, but Shafiq (86 not out) and captain Sarfraz (57no) came to the rescue with an unbroken stand of 146 - a record for the sixth wicket against Sri Lanka.
Pakistan, striving to avoid a first Test series defeat in the United Arab Emirates since they have been unable to play on home soil, closed on 198-5 and require another 119 to complete a remarkable fightback and salvage a 1-1 draw on the final day.
Sri Lanka slumped to 34-5 in a dramatic final session on day three and were six down for 59 when Niroshan Dickwella (21) was well taken by wicketkeeper Sarfraz to become Wahab's fourth victim after the dangerous left-hander struck four boundaries.
Dilruwan followed in the next over when he was trapped in front by a Yasir Shah flipper and Sohail wrapped up the tail in a flash by getting Rangana Herath, Kusal Mendis and Nuwan Pradeep in an astonishing over.
Debutant Lahiru Gamage was left frustrated when he had Sami Aslam caught at second slip early in Pakistan's run chase but was deemed to have over-stepped, before getting rid of the opener in the same manner two balls later.
Pakistan collapsed feebly in their first innings and looked set for a crushing loss when they lost four wickets in the space of nine overs in the second session.
Dilruwan seemingly put Sri Lanka well on their way to victory after Pradeep removed Azhar Ali, Sohail feathering one behind which turned away before the all-rounder saw the back of Shan Masood and Babar Azam in the same over.
Shafiq and Sarfraz both had a stroke of luck when Mendis failed to hold on to sharp chances at short leg in consecutive deliveries, but the pair showed their team-mates how to counter the spin as they batted with increasing assurance.
They used their feet expertly and waited for loose deliveries to be served up, Shafiq bringing up his 19th Test half-century with a sumptuous cut to the boundary and Sarfraz also passing fifty by pulling Mendis for four.
Sri Lanka were unable to conjure up a late breakthrough and lost their second review in the final over of a day which left the outcome in the balance, with the tourists having only seven overs to wait before they can take the new ball on day five.