Pakistan looked to be cruising in their pursuit of South Africa's 183 all out as they progressed to 164 for four in the 41st over, but they then lost their final six wickets for the addition of just 17 runs as the Proteas stormed back to claim the win.
Proteas paceman Wayne Parnell added three top-order wickets to go with his 56 with the bat earlier on but it was Pakistan-born spinner Imran Tahir who caused much of the late devastation as he claimed three of the six wickets to fall, including that of the dangerous Shahid Afridi for nine.
Lonwabo Tsotsobe also took scalps during the collapse before Morne Morkel clinched the win when he bowled Mohammad Irfan for two.
Earlier, Pakistan's spinners looked to have put their side in charge after South Africa had won the toss and opted to bat first.
Slow bowlers Saeed Ajmal (four for 30) and Afridi (three for 37) combined for seven wickets as the Proteas were bowled out with a ball to spare in their innings.
It would have been an even poorer looking effort from AB de Villiers' side if it had not been for number eight Parnell, who struck six fours and a six as he recorded his first international half-century.
The 24-year-old left-armer then impressed with the ball, dismissing Pakistan's three top scorers Ahmed Shehzad (58), Misbah-ul-Haq (31) and Mohammad Hafeez (28) to help keep South Africa in contention with their opponents on 134 for four. It also ultimately helped win Parnell the man of the match award.
However, after Parnell's breakthroughs, Umar Amin (20) and Umar Akmal (18) then added 30 for the fifth wicket and it looked like Pakistan were almost home and dry with only 19 runs needed and plenty of wickets and overs in the bank.
South Africa refused to give up though, and the dismissals of the two Umars in successive balls from Tsotsobe and Tahir gave them a lifeline.
Pakistan still looked overwhelming favourites as they added another nine runs before they lost their seventh wicket, Sohail Tanvir caught behind off Tsotsobe for two, but when Tahir sent back Afridi and Wahab Riaz in the same over, suddenly a remarkable comeback looked a real possibility.
Last pair Ajmal and Imran survived a number of close calls as they scraped together five runs to get to within a single of South Africa's total but, with the finishing line so close, Pakistan fell short as Morkel clipped Irfan's stumps to seal a sensational win.
The second of the five-match ODI series takes place in Dubai on Friday.