Sachin Tendulkar's 51st Test century and couple of late wickets for Harbhajan Singh left the series decider between India and South Africa evenly poised after three days at Newlands.
Tendulkar turned his overnight 49 into a majestic 146 as India were bowled out for 364 in the evening session, a lead of two on first innings.
That was despite a hostile display from Proteas paceman Dale Steyn, who finished with 5-75.
Off-spinner Harbhajan then removed Graeme Smith and nightwatchman Paul Harris - both lbw - to reduce South Africa to 52-2 in their second innings at stumps.
India resumed on 142-2 and had a scare in the first over when an edge from Tendulkar to Mark Boucher was not spotted by either umpire Ian Gould or Steyn, although the ball did appear to bounce millimetres before being caught.
Tendulkar soon went past 50 but both he and Gambhir had plenty of luck as the ball regularly beat the bat and edges flew wide of fielders.
With Steyn out of the attack and Morne Morkel tiring, the runs started to come more freely - the 150 partnership brought up from 302 deliveries, followed by the team's 200 in the 69th over - despite Lonwabo Tsotsobe also impressing.
The breakthrough finally arrived, though, through the spin of Harris - the slow left-armer drawing Gambhir (93) into a prod outside off stump with the edge gratefully held by Boucher, who had dropped a difficult chance the ball before.
There was some fortune in the next wicket, which came minutes before lunch as Tendulkar's straight drive was deflected onto the stumps as Harris let a sharp chance through his fingers, leaving VVS Laxman (15) stranded and India 237-4 at the break.
With the new ball in operation thereafter, Steyn took out Cheteshwar Pujara (2), before Tendulkar extended his record century mark in Tests with a top-edged six off Morkel to raise his ton.
When Steyn returned for his second over, he accounted for Mahendra Singh Dhoni (0) and then in his fourth over the South Africans were adamant that they had Harbhajan and were livid that umpire Gould did not give him out caught behind.
However, replays showed the Englishman was spot on as Steyn's ball took the off stump - amazingly the bails stayed put - rather than an edge on its way through to Boucher.
With the quick men slowly tiring and the ball getting older, the home side seemed to lose intensity as the 50 partnership came up from 97 balls, along with the 300 in the 101st over.
At tea it was 316-6 and Harbhajan fell straight afterwards when his miscued pull off Steyn went to Jean-Paul Duminy running in from deep square-leg.
Tendulkar's innings eventually ended on 146 when Morkel went through his defences and the ball clipped the top of off stump.
Steyn's fiery efforts were then rewarded as he completed his five-for when Ishant Sharma (1) fended a bouncer to Boucher and Zaheer (23) was last man out, just after India edged into a two-run lead.
In reply, Smith (29) and Alviro Petersen (22no) quietly went about their business as South Africa went past 50 in the 14th over.
But in the same Harbhajan over, the South Africa captain was out leg before wicket and then Harris fell 10 deliveries later for a duck, also lbw, to set up a tense last two days.