Murali Vijay (75) scored a defiant fifty before falling to Simon Harmer, as the visitors dominated the first two sessions at the IS Bindra Stadium.
Elgar turned out to be the unlikely hero after India skipper Virat Kohli opted to bat.
Ravindra Jadeja (26 batting) and Ravichandran Ashwin (4 batting) are fighting on to try and give some respectability to the Indian total.
India lost two wickets in quick succession between overs 38 and 40. Rahane was the first to go following an expansive drive off a looping delivery that pitched outside the off-stump, the edge flying to first slip.
Also Read
Elgar struck again on the very first delivery of his next over, sending back wicketkeeper-batsman Wriddhiman Saha to be on a hat-trick.
All-rounder Jadeja played out the hat-trick ball but India's struggle was far from over.
But that was not to be as off-spinner Simon Harmer got the dangerman trapped LBW and the hosts slipped further. Amit Mishra (6) also couldn't resist the temptation against Elgar's looping deliveries and fell while attempting to hit past mid-on.
Ashwin survived a scare after he was trapped LBW off debutant pacer Kagiso Rabada as it turned out to be an over-stepped no ball.