Azhar Ali scored a century and Misbah-ul-Haq an unbeaten 68 as Pakistan chased down 302 to beat Sri Lanka by five wickets in fading light at the end of the third and final Test in Sharjah.
Sri Lanka began a dramatic day as favourites to hold on to their 1-0 series lead but ended it disappointed as Pakistan's batsmen, led by Azhar (103) and Misbah-ul-Haq (66 not out), foiled them to level the series.
With the light fading, Sri Lanka tried to convince the umpires that play should be called off, but instead Angelo Mathews was warned for time-wasting and they were compelled to carry on.
The winning runs, hit by Misbah, came with nine balls remaining and although a direct hit by Dinesh Chandimal would have run Asad Shafiq out, he missed, and Pakistan were able to celebrate.
It is just the second time in their Test history that they have successfully chased a target of more than 300.
Sri Lanka resumed the day on 133 for five in their second innings with a lead of 220 but aside from Mathews' 31 in 128 balls and Prasanna Jayawardene's 49 in 88 made little progress and were bowled out for 214.
Left-arm spinner Abdur Rehman picked up four for 56, off-spinner Saeed Ajmal three for 53 and fast bowler Mohammad Talha three for 65.
The first wicket to fall was Mathews who pulled Talha to Khurram Manzoor in the deep to make it 189 for six as Sri Lanka lost their last five wickets for 25 runs.
Next to go was Dilruwan Perera (8) who was brilliantly caught by Azhar at short leg off Rehman, who then struck next ball to have Rangana Herath caught by Younus Khan at slip.
There was to be no hat-trick for Rehman but Pakistan did not have to wait long to wrap up the innings with when Ajmal had Jayawardene caught by Azhar and Shaminda Eranga (3) caught by Rehman.
That set Pakistan 302 to win in 59 overs and they made a fast start through openers Ahmed Shehzad and Manzoor, who both made 21.
However, when both were dismissed by Suranga Lakmal to leave the score on 48 for two in nine overs, Sri Lanka would have collectively breathed a sigh of relief.
Younus Khan (29) then fell in the 22nd over, caught by Kumar Sangakkara off Mathews to make it 97 for three, bringing Azhar and Sarfraz Ahmed together.
While Azhar did not exactly drop anchor, he played a more cautious role than Sarfraz, who hit four fours and a six on his way to 48 in 46 balls to resuscitate the run chase until he gloved Eranga to wicket-keeper Jayawardene.
The pair had put on 89 together and the match was back in the balance, although Pakistan still had plenty of work to do from 186 for four.
Azhar and Misbah then batted together for one ball shy of 20 overs and it was their 109-run partnership that turned the game decisively in Pakistan's favour.
Azhar reached his fifth Test century and as Sri Lanka opted - unwisely, perhaps - to put their fielders on the boundary, both players worked the singles, found the gaps and scored boundaries when they could.
Soon after he had reached three figures, Azhar fell, caught behind off Lakmal as he looked to go for a big shot, but the game was all but won with seven more required.
They came quickly through Shafiq (1 not out) and Misbah, who played a captain's knock of 68 not out in 72 balls with four fours.
The opening Test in Abu Dhabi was drawn before Sri Lanka won the second Test in Dubai by nine wickets.
© Cricket World 2014