Kagiso Rabada claimed the vital wicket of Usman Khawaja shortly before stumps after his fine fifty had frustrated South Africa's push for victory on day three of the second Test in Port Elizabeth.
Australia closed 41 runs ahead on 180-5, but the loss of Khawaja (lbw for 75) in the penultimate over of the day was a major blow to their hopes of earning a positive result from the Test.
Earlier, AB de Villiers (126no) notched a magnificent 22nd Test ton as South Africa added a further 119 runs to their overnight lead of 20 before eventually being bowled out for 382 on the stroke of lunch.
The Proteas then appeared to be heading to a comfortable victory, taking wickets at regular intervals in the afternoon session, including the key scalps of David Warner (13) and Australia captain Steve Smith (11).
Warner was clean-bowled for the second time in the Test, this time by Rabada (3-38), and Cameron Bancroft (24) rather unfortunately chopped a Lungi Ngidi (1-21) delivery onto his stumps, before Keshav Maharaj (1-70) had Smith caught behind.
Rabada - who might well find himself in further hot water, having given Warner a vocal send-off after taking his wicket - then struck with the second ball after tea, as Shaun Marsh (1) feathered an edge through to Quinton de Kock.
Rabada has already been charged by match officials for misconduct during the Test, having shoulder bumped Smith following his dismissal on day one. The South African fast bowler faces a two-Test ban if found guilty.
At the fall of Shaun Marsh's wicket, Australia were still some 53 runs behind their hosts, but Mitchell Marsh and Khawaja helped the tourists into a slender lead.
The pair put on 87 for the fifth wicket and seemed to be safely guiding Australia through to stumps without further loss, until Rabada stepped up once more to claim his eighth wicket in the Test.
Australia lead the four-Test series 1-0 after their 118-run win in Durban.
Watch day four of the second Test between South Africa and Australia, live from 7.55am on Monday on Sky Sports Cricket.