The Pakistan captain hit a stroke-filled 103 off 104 balls but his dismissal started a collapse in which fast bowler Anrich Nortje took four wickets in four overs.
Rassie van der Dussen hit 123 not out, his maiden international century.
But South Africa’s total of 273 for six did not seem enough as Babar and Imam-ul-Haq (70) put on 177 for Pakistan’s second wicket, taking them to within 88 runs of victory with 18.2 overs left before Babar was caught behind off Nortje.
Four wickets fell for 17 runs.
Mohammad Rizwan and Shadab Khan steadied the innings in a partnership of 53 but both were dismissed in the closing overs before Faheem Ashraf hit the winning run off the final ball.
Pakistan captain Babar Azam earlier said the presence of some moisture in the pitch influenced his decision to bowl first. Pakistan gave a first cap to batsman Danish Aziz.
Temba Bavuma, captaining South Africa for the first time, said the hosts would seek to post a total of about 280.
“There might be something in the wicket for the bowlers but on this ground we can score quite a lot at the end if there are wickets in hand.”
Both teams opted for pace-based bowling attacks, with three specialist fast bowlers, one spinner and a seam-bowling all-rounder.
The three-match series is part of the International Cricket Council’s World Cup Super League.
(This story has been published from Such TV feed, without modifications to the text)