Pakistan stuttered over the line in Abu Dhabi to record a shock seven-wicket victory in the first Test of their two-match series with South Africa.
The Proteas were dismissed for 232 on day four to set their opponents a paltry victory target of 40, which Pakistan ultimately chased down with ease despite being reduced to 7-3.
The defeat marks the first time South Africa have lost a Test match in almost two years and the world's top-ranked side must win the second clash if they are to avoid their first series defeat since early 2009.
However, the result did not quite look a foregone conclusion after a top-order collapse left Pakistan floundering in the fourth over.
Opener Shan Masood (0) was caught behind by AB de Villiers during the first over of Vernon Philander, who accounted for Khurram Manzoor (4) via the same method in his next.
In between, Azhar Ali (3) had been dismissed by Dale Steyn, but experienced heads Younis Khan and Misbah-ul-Haq stepped in to calm any fears of a calamitous defeat.
Younis made an unbeaten nine from 36 balls but skipper Misbah, who scored a century in the first innings, cut loose to score 28 not out from 26, including a booming six which completed the win.
South Africa had started the day needing 121 more runs to avoid an innings defeat, something which seemed a distinct possibility when nightwatchman Steyn (7) and JP Duminy (0) fell in quick succession.
But De Villiers struck a defiant 90 and Robin Peterson chipped in with 47 not out to ensure the Proteas escaped that embarrassment.
De Villiers launched Zulfiqar Babar down the ground to bring up his half-century before lunch, which he reached alongside Faf du Plessis.
Du Plessis' knock of nine was brought to an end shortly after the interval when he drilled one down the ground only to see Saeed Ajmal throw out a hand and grab a fine return catch.
De Villiers and his new support man, Peterson, compiled a busy fifty partnership before Junaid Khan (3-57) came back into the attack to remove the wicket-keeper 10 runs short of his century.
Peterson's hard work, assisted by Adnan Akmal's drop, looked to have taken the innings to tea only for Ajmal, who finished as the pick of the bowlers with 4-74, to snare Vernon Philander (10) and Morne Morkel (0) in the space of three deliveries right at the end of the afternoon session.