The Springboks were beaten 24-23 in Perth three weeks ago and knew another loss at Newlands would see them relinquish second spot in the championship table and the world rankings to their opponents.
A first Australia win in Cape Town since 1992 appeared to be on the cards when the visitors led 10-8 with 12 minutes remaining.
However, replacement fly-half Pat Lambie then atoned for an earlier penalty miss with a successful drop-goal attempt and South Africa went on to finish with a flourish.
Captain Jean de Villiers, in what appears likely to have been his final Test outing at Newlands, crossed twice and Lambie also claimed a try as the hosts secured an unlikely bonus point to move within a point of leaders New Zealand, who face Argentina on Sunday.
Prior to South Africa's late surge, a first-half try from Adam Ashley-Cooper had looked set to prove decisive. Yet Australia were ultimately beaten in convincing fashion and now trail the Springboks by five points ahead of the final round of fixtures.
Heyneke Meyer's men claimed an 12th-minute lead when a rolling maul from a lineout resulted in Marcell Coetzee being driven over.
The subsequent conversion attempt was missed by Handre Pollard and Bernard Foley reduced Australia's arrears with a penalty before powerful play from Tevita Kuridrani saw the visitors move ahead.
Kuridrani showed great strength to break through the challenges of two opponents, injuring Bryan Habana in the process, and then switched the ball to the right for Ashley-Cooper to cross under pressure in the corner.
A penalty from Pollard brought the Springboks back within two points early in the second half, but Lambie missed the target with a relatively straightforward kick at goal in the 64th minute.
However, he soon made amends with a drop-goal that followed 31 phases of possession and De Villiers was over in the right corner two minutes later following Jan Serfontein's wonderful break and fine work from Cornal Hendricks.
Another missed kick from Lambie mattered little as the 23-year-old grabbed South Africa's third try with two minutes remaining.
Remarkably, there was still time for De Villiers to go over on the left to seal a bonus point as Australia's tiring players flagged badly.