During his knock of 160, including 18 fours and four sixes, Elgar forced India to constantly rethink their plans.
The 32-year-old brought up his century with a slog-sweep six off Ashwin in the 60th over of the innings and became the first South Africa batsman to score a Test hundred in India after nine years.
Hashim Amla was the last batsman from South Africa to score a hundred in India during their 2010 tour.
India were in absolute control at the close of play on day 2 as Ravichandran Ashwin picked up two wickets, while Ravindra Jadeja removed night-watchman Dane Piedt.
Resuming at 39 for 3 on day 3, the South Africa opener forged vital partnerships with captain Faf du Plessis and wicketkeeper Quinton de Kock to help the visitors reach 385 for 8 at stumps. de Kock also scored his 5th Test century.
South Africa still trail India by 117 runs but they managed to put pressure on the hosts at crucial junctures.