AB de Villiers and Faf du Plessis batted throughout the afternoon session to stave off defeat and with 127 needed in a minimum 30 overs after tea, an improbable home win was still possible.
The pair each pushed on to their centuries and South Africa appeared favourites to win until Du Plessis was run out for 134. Vernon Philander and Dale Steyn left it too late to pursue the 16 runs still needed and their side finished eight shy on 450 for seven.
Resuming on 138 for two, the home side suffered an early blow when opener Alviro Petersen was bowled by Mohammed Shami off an inside edge without adding to his overnight 76.
Zaheer Khan then had Jacques Kallis lbw for 34 - unluckily, having also inside-edged the ball - to leave South Africa on 197 for four. It was Zaheer's 300th wicket in Tests.
Du Plessis and De Villiers ensured the hosts reached the break without further loss, though, and pressed on through the afternoon session.
Their 50 partnership came up shortly after the restart and both players then reached the landmark individually - Du Plessis from 142 balls with four fours while De Villiers took 84 balls to get there with eight boundaries.
De Villiers inside-edged a four to bring up the century stand and the team's 300 soon followed when Du Plessis diverted a vicious lifter from Zaheer past the slips.
By the time tea came around, Du Plessis was on 88 with De Villiers on 71 and the former soon reached three figures with a clip to deep mid-wicket for three off Zaheer.
De Villiers' century - 90 balls quicker than his partner's at 162 balls, with each hitting 12 fours - was greeted with an understated celebration, indicating the victory chase was uppermost in his mind.
But he added only three more runs before chopping on to give Ishant Sharma his first wicket.
JP Duminy played on against the dangerous Shami (three for 107) for five but Philander hit Shami for four to keep South Africa within range and eight runs came off the 132nd over, meaning 20 were needed from the last four.
But Du Plessis was crucially run out, Ajinkya Rahane hitting the stumps as he had on day four to dismiss Graeme Smith, and made a devastated walk back to the dressing room.
Steyn was unable to score against some short-pitched bowling from Shami - and the lower-order pair were playing out time until first Steyn then Philander missed with wild hoiks at the third and fourth balls of the last over.
Philander jogged a long single off the next before a bizarre finish saw Steyn, having blocked his way to the end, hoist the final ball down the ground for a huge but meaningless six.
The West Indies' 418 to beat Australia in 2003 therefore remains the highest successful chase in Tests, though South Africa's was the third-highest fourth-innings total.
Captain Graeme Smith told Sky Sports 3: "I thought we played it beautifully up until the moment Faf was run out, then you've got to back the guys' decisions out in the middle.
"Chasing this down would have been incredible but when we started today, we wanted to set up our stall and make sure we went to Durban with an opportunity to win the series, and we've done that.
"I thought Faf and AB's knock was incredible, they'll go down in history as some of the greatest knocks in the game.
"To see Faf come out and bat at four and produce a performance like that was tremendous for us. He had a lot of pressure on his shoulders going into this game and he's answered his critics superbly again."
Opposite number Mahendra Singh Dhoni added: "In the middle (session) we attacked a bit more than we should have but I was quite confident in our bowlers that if five, or five and a half, runs an over was needed, they would be able to defend that.
"There was a situation where we could easily have lost this game but it was a big learning for the fast bowlers, next time we are in a situation like this they'll know how to bowl and how much to attack."
Philander finished 25 not out, to add to his first-innings 59 and match figures of seven for 129.
India were thankful to the batting of Virat Kohli, who made 119 and 96, and Cheteshwar Pujara for his 153 in their second innings - but most of all to Rahane's throwing arm.