Pakistan completed the upset of top-ranked South Africa in Abu Dhabi they had threatened for the last two days, albeit after a minor wobble in pursuit of 40 to win.
Misbah-ul-Haq saw his side slip to seven for three, but struck 28 at better than a run a ball to complete victory for no further loss and with over a day to spare.
Pakistan going ahead in the two-game series, the first time the Proteas have lost a Test since November 2011, is all the more remarkable given the sixth-ranked team were held to 1-1 draw by lowly Zimbabwe last month.
Trailing by 121 overnight on 72 for four, South Africa owed much to 90 from AB de Villiers and Robin Peterson’s unbeaten 47 to avoid an innings defeat.
Zulfiqar Babar bowled nightwatchman Dale Steyn cheaply and Junaid Khan trapped JP Duminy plumb in front for nought, but de Villiers raised his fifty with a straight six off Babar in the over before lunch.
Soon after the interval Saeed Ajmal snared Francois du Plessis caught and bowled, only for de Villiers and Peterson to share 57 until Junaid had the former caught at cover.
Peterson put the Proteas in front before attacking against the old ball and immediately-taken new one. However, Ajmal ousted Vernon Philander and Morne Morkel in the space of three balls to herald tea and for figures of 4-74.
Pakistan lost their wickets in consecutive overs, Shan Masood and Khurram Manzoor caught behind off Philander, Masood for a duck, and Azhar Ali edging Steyn to slip.
However, Misbah hit Peterson for two sixes down the ground, the latter ending proceedings.