England were 48 without loss in their second innings at tea on the third day -- an overall lead of 145 runs.
Cook, in his first Test since resigning as captain in February, was 31 not out and fellow left-handed opener Keaton Jennings 16 not out.
Vernon Philander had yet to bowl in the innings after going for an X-ray on a hand injury suffered when he was struck by James Anderson while making 52 in South Africa's first innings 361 earlier on Saturday.
Cook had made just one when given not out by umpire Paul Reiffel in response to Morne Morkel's lbw appeal.
More From This Section
South Africa, with Dean Elgar captaining the side in the asbence of Faf du Plessis on paternity leave, reviewed.
Cook had moved on to 12 when a delivery from left-arm spinner Keshav Maharaj turned sharply into his pads.
South Africa reviewed again but, with replays showing the ball had struck Cook outside the line of off-stump when he was trying to play a shot, Indian umpire S Ravi's decision was upheld on this occasion.
South Africa resumed on 214 for five -- still 244 runs behind an England total which featured Joe Root's 190 in his first Test as England captain as well as fifties from Moeen Ali (87), Stuart Broad (57 not out) and Stokes (56).
Bavuma completed his fifty when he square-drove Stokes for four.
England, fielding two spinners in a Lord's Test for the first time since 1993, then saw both their slow bowlers take wickets.
Hampshire left-armer Liam Dawson, on his home debut, made the breakthrough with just his second ball Saturday when Rabada (27) edged to wicket-keeper Jonny Bairstow.
Off-spinner Ali, who on Thursday completed both halves of the Test all-rounder's double of 2,000 runs and 100 wickets, had Bavuma (59) taken at slip by Stokes via Bairstow's thigh.
But wicket-keeper Quinton de Kock twice struck Broad for three fours in as many balls -- the best a whipped drive through mid-on from a ball pitching outside off stump.
It was a typically dashing display by the left-hander, who completed the second-fastest Test fifty at Lord's in a mere 36 balls, including 10 boundaries.
But his next ball proved his last, de Kock slicing Anderson to square cover where Stokes took an excellent low catch.
Philander made an 80-ball fifty before he was last man out slogging at Ali, who finished with impressive figures of four for 59 in 20 overs.