Pakistan completed a brilliant chase of 302 in two sessions to win the third Test in Sharjah and draw their series against Sri Lanka.
After their 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, Misbah hitting the winning run in fading light with nine balls to spare.
It was Pakistan's second-highest chase in Tests, bested only by their 315 for nine to beat Australia in Karachi 20 years ago.
Sri Lanka, needing only 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 and their side's lead to 276.
But the dismissal of Mathews, caught by 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 (four for 56) and Saeed Ajmal (three for 53) did the damage with two wickets apiece, with the latter accounting for Jayawardene after the wicketkeeper had top-scored with 49.
Lunch was taken immediately after the dismissal of last man Shaminda Eranga to set up an exciting chase of 302 in 59 overs.
Openers Khurram Manzoor and Ahmed Shehzad each contributed 21 before both fell to Suranga Lakmal, while Younis Khan (29) also kept the scoreboard ticking over.
His dismissal saw wicketkeeper Sarfraz Ahmed promoted to number five and the Twenty20 specialist gave a sign of things to come with a boundary just before tea.
He took Herath for 14 from the first three balls of the 29th over, culminating in a huge six over mid-wicket, and made 48 from 46 balls before falling to a nick down the leg side, detected after a Sri Lankan review.
Azhar had reached a 79-ball half-century shortly beforehand and he was joined by Misbah, the man for any situation.
The chase dictated the skipper would bat in the higher of his two wildly contrasting gears, and he picked off boundaries where available to tick along at a run a ball.
The 50 stand took 56 balls, Misbah contributing 30, and the skipper's 59-ball half-century came up with 5.3 overs remaining and the required rate standing at a run a ball.
Azhar's century was the next milestone and after a crucial boundary in the 52nd over prevented the rate climbing too prohibitively, he hit 10 runs from four balls in the 55th to move to three figures.
That meant 17 runs were needed from four overs and Misbah's single brought up the century stand from 111 balls, with Sri Lanka beginning to argue for a stoppage due to bad light.
With 12 needed from three overs, Misbah drilled a four but man of the match Azhar was then dismissed for 103, caught behind after his nick skimmed the stumps but did not take the bails off.
He ran back to the pavilion so as not to waste time and four runs came off the remainder of the over, Misbah keeping the strike and then finishing the job in the next to spark wild celebrations.