Pakistan pulled off a thrilling final day run-chase to beat Sri Lanka by five wickets and square the three-match Test series in Sharjah.
Requiring 302 for victory in 59 overs after dismissing Sri Lanka for 214 in their second innings on the stroke of lunch, Pakistan were indebted to 103 from Azhar Ali and Misbah-ul-Haq's unbeaten 68 as they crossed the line in fading light with nine balls to spare.
It was Pakistan's second-highest chase in Tests, bested only by their 315-9 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-5, they were bowled out in the morning session as Pakistan, through Abdur Rehman's four-wicket haul, 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 Rehman (4-56) and Saeed Ajmal (3-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.