Guptill finished with 101 not out off 69 balls, hitting the last ball of the match from Rory Kleinveldt over cover for four to enable New Zealand to reach a target of 169 in a match reduced to 19 overs a side because of a floodlight failure.
The result enabled New Zealand to level the three-match series with the decider to be played in Port Elizabeth on Wednesday.
VIDEO: South Africa v New Zealand: 2nd T20I
Guptill missed his side's eight-wicket defeat in the first match in Durban on Friday because of a stomach ailment.
But the opening batsman showed his value to the team by slamming six sixes and eight fours.
He became the second New Zealander - after captain Brendon McCullum - to hit a century in a Twenty20 international.
He had some good fortune in a frantic finish. He was caught off Ryan McLaren when he was on 87 but gained a reprieve when the delivery was no-balled because it was too high.
Two runs later he hit a ball from Morné Morkel high in the air towards point but Robin Peterson put down the chance.
South Africa scored 165 for five after a floodlight failure caused a 50-minute delay.
The home side were 128 for two after 16.1 overs when the lights on one of four floodlight towers went out.
Henry Davids hit 55 and captain Faf du Plessis made 63. New Zealand seam bowler Doug Bracewell took three wickets in four balls in the final over, all from catches in the deep, to finish with three for 33.
Du Plessis, who made his runs off 43 balls, shared partnerships of 68 for the second wicket with Davids and 79 for the third wicket with David Miller, who hit 33 off 18 deliveries.
Davids,
playing in only his second international match, made a maiden fifty
before falling to a sensational catch by Jimmy Neesham, running back and
diving full length to hold a lofted drive just inside the long-off
boundary.
Brief scores:
New Zealand 169-2 in 19 overs (Martin Guptill 101 not out, Rob Nicol 25) def. South Africa 165-5 in 19 overs (Faf du Plessis 63, Henry Davids 55; Doug Bracewell 3-33)