Sri Lanka consigned Pakistan to their first series defeat in the United Arab Emirates, prevailing by 68 runs as Asad Shafiq's century proved in vain in the second Test in Dubai.
After conceding a first-innings deficit of 220, Pakistan looked certain to fall to a heavy loss but they dragged themselves back into contention by bowling out the tourists for 96 before Shafiq and Sarfraz Ahmed (68) put on a record sixth-wicket partnership on day four.
That same duo came to the crease on Tuesday with 119 still needed but Sri Lanka remained patient, Dilruwan Perera's 5-98 helping to secure the series 2-0 after a similarly engrossing 21-run success in the opener in Abu Dhabi.
After Sarfraz fell early, Shafiq - whose previous highest score this year was 39 - represented the Pakistan's last vestige of reaching their victory target of 317.
But when he fell for 112 to an excellent Kusal Mendis slip catch, Pakistan were nine down with 73 still needed and it was left to Rangana Herath to finish off the home side for 248 and wrap up a ground-breaking series win - a first for a touring team since Pakistan moved from playing Tests on home soil.
It was tough on Shafiq to finish on the losing side, the batsman hitting his first ball of the day for four to bring up the 150 partnership and see his team past the 200 mark.
A paddle to fine leg for a single brought up his 11th Test hundred, before he was dropped from his next delivery by Dimuth Karunaratne at slip off the bowling of Dilruwan, whose disappointment was soon alleviated as Sarfraz top edged a sweep and was well held at fine leg by Nuwan Pradeep.
Dilruwan trapped Mohammad Amir lbw for four, the batsman unsuccessful with his review, but Pakistan had more joy with the use of DRS in the following over as Shafiq managed to overturn Nigel Llong's decision after debutant Lahiru Gamage nipped one back into his pads.
Yasir Shah (5) was next to go, Herath luring him out of his crease allowing Niroshan Dickwella time to juggle before effecting the stumping.
Two balls later, Shafiq edged Suranga Lakmal to Mendis and the last word went to Herath as Wahab Riaz (1) skied one to captain Dinesh Chandimal, who led the raucous Sri Lanka celebrations.