Port Elizabeth: Kagiso Rabada struck shortly before the close to restore South Africa's ascendancy on the third day of the second Test against Australia at St George's Park on Sunday.
Australia were 180 for five in their second innings at the close, a slender lead of 41 runs with five wickets remaining.
A magnificent century by AB de Villiers enabled South Africa to take a first innings lead of 139 and Australia lost four wickets before they wiped out the deficit.
But Usman Khawaja and Mitchell Marsh shared a fighting fifth wicket partnership of 87 and almost succeeded in taking their team through to the fourth day with at least a glimmer of hope.
Then Rabada struck for the third time in the innings. Bowling around the wicket he trapped Khawaja in front of his stumps with a full delivery after the left-hander made 75 off 136 balls.
Khawaja and Marsh (39 not out) came together when Australia were 86 for four but batted for more than two hours on a pitch which was largely placid. In contrast to the first innings, there was no pronounced reverse swing for the bowlers.
De Villiers made a magnificent 126 not out and had good support from the tail as South Africa scored freely before they were all out for 382 shortly before the scheduled lunch break.
South Africa then started well with the ball.
Fast bowler Rabada, under threat of a suspension which could rule him out of the rest of the four-Test series, made a key breakthrough when he bowled David Warner for 13 during a hostile opening spell in which his fastest delivery was timed at 151 kmh.
Cameron Bancroft was bowled off an inside edge by Lungi Ngidi for 24 and captain Steve Smith was caught behind off left-arm spinner Keshav Maharaj shortly before tea for 11.
It was the third time in four innings that Smith, the number one Test batsman in the world, had fallen to left-arm spin - twice to Maharaj and once to part-timer Dean Elgar.
Rabada struck again when he had Shaun Marsh caught behind for one with the second ball after tea but the South Africans had to toil, having unsuccessful leg before wicket reviews against both batsmen, before Rabada's late strike.
Earlier, Vernon Philander (36) and Maharaj (30) helped De Villiers add 84 for the eighth wicket and 58 for the ninth wicket as South Africa dominated a sunny morning.
Australia's fast bowlers, who had a heavy workload on the previous day, wilted under an assault in which De Villiers played strokes that were sometimes breathtaking.
He hit 20 fours and a six in a 146-ball innings. It was his 22nd Test century and his sixth against Australia.
Philander played a solid innings, while Maharaj slammed two sixes and three fours off 24 balls. He appeared to have been caught when he hit his first six off Nathan Lyon but Khawaja stepped over the boundary before tossing the ball back infield to complete the catch.
South Africa added 119 runs off 23.4 overs before a direct hit from Smith in the covers ran out last man Ngidi at the bowler's end as the batsmen tried a risky second run to keep De Villiers on strik.