Pakistan completed a brilliant chase of 302 in two sessions to win the third Test at Sharjah and draw their series against Sri Lanka.
After the bowlers had fought back in the morning session to set up the run-chase, Pakistan's top order scored at 4.45 an over in the afternoon to keep themselves in contention.
They still required 195 runs in the evening but captain Misbah-ul-Haq and centurion Azhar Ali judged the pace to perfection, the former hitting the winning run in fading light with nine balls to spare as Pakistan won by five wickets.
It was Pakistan's second-highest chase in Tests, bettered only by their 315 for nine to beat Australia in Karachi 20 years ago.
Sri Lanka, needing just a draw to win the series 1-0, looked in firm control at the start of the day as they boasted a lead of 220 with five second-innings wickets in hand.
However, after taking the field on 133 for five, they were bowled out for 214 as Pakistan gave themselves a glimmer of hope.
Sri Lanka appeared to be cruising along with Angelo Mathews and Prasanna Jayawardene steadily extending their overnight partnership to 62.
But the dismissal of Mathews, caught by Khurram Manzoor at deep square-leg off paceman Mohammad Talha for 31, sparked a collapse as Sri Lanka lost their last five wickets for 25 runs.
Spinners Abdur Rehman and Saeed Ajmal, with 4-56 and 3-53 respectively, did the damage.
Pakistan openers Manzoor and Ahmed Shehzad each contributed 21 before both fell to Suranga Lakmal, while Younus Khan also kept the scoreboard ticking over with 29.
Wicketkeeper Sarfraz Ahmed, promoted to number five, produced a fine cameo of 48 at better than a run a ball to offer further hope.
Their optimism was to prove justified as Misbah and Azhar starred thereafter to seal a stunning win, scoring 68 not out and 103 respectively to give Dav Whatmore the perfect send-off as coach.