It was an unconvincing display from the Springboks who were expected to crush an injury-ravaged Scottish side but had to scramble desperately to grind out the victory.
Scotland scored the first two tries and were 17-6 up early in the second half before South Africa clawed back with three tries.
The nippy skills of captain Greig Laidlaw laid the platform for much of Scotland's enterprise as South Africa's much-vaunted defence was made to looked ragged.
Scotland were 10-6 ahead at halftime after Laidlaw and debutant winger Tommy Seymour burst through the home side's midfield and lock Tim Swinson almost took the ball over the line before Matt Scott scored from the resultant maul.
Loose defence allowed Swinson to drive forward again three minutes into the second half before the ball went out wide for Alex Dunbar to score on only his second international appearance.
A missed penalty by the normally reliable Morne Steyn laid bare the home side's anxiety before a drive by the pack on to the line forced French referee Romain Poite to award the home side a penalty try.
The turning-point came when Scotland lock Jim Hamilton's irritation with his opposite number Eben Etzebeth boiled over with a push into the face 12 minutes into the second half.
After a review of the television replays, he was shown a yellow card, leaving the visitors one short as the home side began to launch their comeback.
It was during Hamilton's time in the sin bin that the Bok backline finally got moving for the first time in the game and after several phases set up centre JJ Engelbrecht to burst through for his second try in successive Tests.
That put South Africa ahead for the first time at 20-17 and a penalty from substitute Pat Lambie and a try on the whistle from 20-year-old centre Jan Serfontein completed the scoring.
Injuries continue to mount for Scotland who flew out three replacements after a bruising defeat by Samoa in Durban last weekend.
Ruaridh Jackson, his replacement Peter Horne and loose forward Ryan Wilson are all doubtful for the next match against Italy in Pretoria.
Earlier on Saturday, Samoa outscored Italy five tries to one to win 39-10 at the Mbombela Stadium.
- Sports & Recreation
- Greig Laidlaw
- South Africa