Australia were 180 for five in their second innings at the close, a slender lead of 41 runs with five wickets remaining.
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.
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.
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.
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.
(This story has not been edited by Business Standard staff and is auto-generated from a syndicated feed.)