The Proteas batted past tea at the Wanderers to reach 229 all out and a slender 27-run lead.
Temba Bavuma notched up a half century, while Keshav Maharaj (21) and Marco Jansen (21) ensured the South Africans could match India's score and then edge ahead.
The Proteas started after lunch with Temba Bavuma (51) and Kyle Verreynne (21) without scoring and still trailing by 100 runs. The two went on to put together a strong partnership of 60 off 122.
Their stand was broken when Verreynne was trapped in front by the by impressive Thakur, who claimed his fourth wicket of the day and the South African middle order started to crumble.
Bavuma went on to register his 17th Test half-century but was needlessly dismissed the following ball when he was caught by Pant off the bowling of Thakur.
Thakur claimed his first five-wicket haul and joined Anil Kumble, Javagal Srinath, Shanthakumaran Sreesanth, Jasprit Bumrah and Mohammed Shami as Indian bowlers who have taken five wickets in an innings in SA.
Shami joined the party as Kagiso Rabada was caught by Mohammed Siraj at mid-on without scoring.
Before lunch, Thakur claimed all the wickets of Dean Elgar (28), Keegan Petersen (62) and Rassie van der Dussen (1).