From reeling at 21 for three inside the first hour, New Zealand were all out for 268 in the final session with the left-handed Nicholls providing the backbone of the innings with 118.
Elgar went for nine and Cook for three to leave nightwatchman Kagiso Rabada on eight with Hashim Amla yet to score.
On a day in which 12 wickets fell, Nicholls' bold innings not only rescued New Zealand but also silenced his critics.
He went into the Test with an average of 30.12 from 18 innings and his position under scrutiny as a number five batsman and responded with an innings of confident drives and cuts against the quicks and sure footwork against spin.
In one notable over from Rabada, a central figure in the top order collapse, Nicholls smacked three fours.
The second took him past his previous best 98 and to his breakthrough century, while the next four lifted his partnership with BJ Watling to 103 to better one of the longest-standing New Zealand cricket records.
When South African captain Faf du Plessis won the toss he put New Zealand into bat noting conditions were ripe for seam and swing and he was not let down.
After Morne Morkel dismissed Tom Latham for eight, Rabada captured the key wicket of Kane Williamson for two and four balls later had debutant Neil Broom for a duck which brought Nicholls to the middle.
Keshav Maharaj removed Jeet Ravel (36) and Jimmy Neesham (15) either side of the lunch break before Watling joined Nicholls to add 116 before New Zealand's batting anchor was bowled by JP Duminy.
It triggered a brief collapse in which three wickets fell for five before Tim Southee and Jeetan Patel lashed out with a whirlwind 44 off as many balls.
Duminy added the wickets of Colin de Grandhomme for four and Watling for 34 in the space of 11 balls and finished the innings when he had Neil Wagner leg before wicket for two and boasting figures of four for 47.
Morkel ended Southee's brief cameo of 27 off 30 deliveries while Patel was left not out 17.
(This story has not been edited by Business Standard staff and is auto-generated from a syndicated feed.)