The 22-year-old Ayub hit his second ton in his seventh ODI with an elegant 109 off 119 balls to lay the foundation for Tuesday's win as Pakistan scored 7-242 with three balls to spare in reply to South Africa's 9-239 off their 50 overs at Boland Park in Paarl.
A fifth-wicket stand of 141 runs off 133 balls with Salman Ali Agha turned the game in the visitors' favour as they struggled initially to score and fell behind the required run-rate as the Proteas' bowlers held early sway.
Pakistan opener Saim Ayub celebrates scoring his second ODI century. (AP PHOTO)
Saim battered South Africa quick Ottniel Baartman for 17 runs in four balls in the 35th over to kick-start the chase and offer Pakistan sight of the finish line.
Ayub, who struck 10 fours and three sixes, holed out to a catch at deep fine leg off Kagiso Rabada, with 39 runs needed and eight overs remaining.
Two more wickets then fell quickly to put the brakes on Pakistan's march, and offered home fans hope of an unlikely comeback, but Salman's unbeaten 82 saw Pakistan home in a close finish.
South Africa, who won the toss and chose to bat, slumped from 0-70 to 4-88 as part-time offspinner Salman ran through their top order, finishing with 4-32 from eight overs.
The hosts had Heinrich Klaasen to thank for being able to post a competitive total as he hit 86 from 97 balls to steady the innings in a 73-run partnership for the fifth wicket with Aiden Markram, who scored 35.
The second ODI is at Newlands in Cape Town on Thursday.