Fakhar Zaman's sensational innings of 193 from 155 balls came in vain as South Africa held on in a thriller to take their three-match ODI series with Pakistan to a decider.
Pakistan seemed out of contention as they slipped to 120-5 in the 25th over of their chase of 342 - only for opener Fakhar to turn the game on its head with a superb fifth ODI ton and the highest score at Johannesburg.
The left-hander brought up three figures from a fairly sedate 107 balls but then blazed another 90 runs from his next 48 deliveries in an innings featuring 10 sixes and 18 fours as he scored 162 more runs than Pakistan's next highest-scorer Babar Azam (31).
Fakhar's onslaught - which included three sixes in a row off spinner Tabraiz Shamsi in the 44th over - left Pakistan requiring 38 from 12 balls with two wickets in hand but Andile Phehlukwayo then shipped only seven runs from the 49th over meaning the tourists needed 31 from the last.
Fakhar was then run out from the first ball of Lungi Ngidi's final over in bizarre fashion, with Aiden Markram completing a direct hit from long-off as Proteas wicketkeeper Quinton de Kock pointed to the bowler's end as if the ball was heading in that direction, deceiving Fakhar.