South Africa claimed five wickets in the last 15 overs to leave the visitors, who had been set 364 for victory, teetering on the brink of defeat on 218 for nine.
Earlier, South Africa were favourites to win when they reduced England to 27 for three inside the first hour on the final day.
But the South African-born duo of Kevin Pietersen [ Images ] (81) and Jonathan Trott (69) seemed to have guided England to safety with their steely fourth-wicket partnership of 145.
Pietersen, who came in after the fall of Alastair Cook [ Images ], looked well on course for his third century in only five Tests against the South Africans.
However, he ram himself out in the second over after tea, opening up an end through which Friedel de Wet, assisted by the second new ball, burst through in an inspired spell of three for 11 in seven overs.
Left-arm spinner Paul Harris and paceman Morne Morkel [ Images ] each chipped in with a wicket apiece as the match concluded in an electrifying atmosphere.
South Africa (first innings): 418
England (first innings): 356
South Africa (second innings): 301
England (second innings overnight 1-5):
A Strauss c Boucher b Morkel 1
A Cook c Smith b Harris 12
J Anderson c Boucher b de Wet 12
J Trott c de Villiers b de Wet 69
K Pietersen [ Images ] run out 81
P Collingwood not out 26
I Bell c Boucher b de Wet 2
M Prior c Boucher b de Wet 0
S Broad c Boucher b Harris 0
G Swann lbw b Morkel 2
G Onions not out 1
Extras: (b-10, lb-3, nb-11) 24
Total: (9 wickets; 96 overs) 228
Fall of wickets: 1-5, 2-16, 3-27, 4-172, 5-205, 6-207, 7-208, 8-209, 9-218.
Bowling: Ntini 18-7-41-0, Morkel 18-3-46-2, Harris 26-11-51-2, de Wet 23-8-55-4, Duminy 8-2-17-0, Kallis 3-1-5-0.