Fullback Damian Willemse has kicked a pressure penalty after the hooter to secure South Africa a 32-29 victory over ill-disciplined Wales in a see-saw Test at a raucous Loftus Versfeld.
The tourists took the world champions close on Saturday, but paid for cynical play in their own 22 as they lost four players to yellow cards and conceded a penalty-try.
The Springboks were playing in front of spectators at home for the first time in three years because of coronavirus restrictions. The Welsh silenced them early.
They led 18-3 at halftime with the help of two Louis Rees-Zammit tries, and blunted the world champions against all expectations for the first 40 minutes at Loftus Versfeld.
"They stopped our mauls and we couldn't get going, but the way we came back, that's what we take pride in," South Africa captain Siya Kolisi said.
South Africa ground back into the game in the second half through the forward pack, with rolling maul tries from hooker Bongi Mbonambi and his replacement, Malcolm Marx.
Wing Cheslin Kolbe added a third then a penalty try from a lineout maul which was collapsed gave South Africa the lead for the first time in the 75th minute, 29-24.
But Wales, who were down to 12 men at one point with three players in the sin-bin in the final minutes, somehow found enough grit to score from their own rolling maul and force South Africa to scramble desperately for a victory.
Former captain Alun Wyn Jones came back from the bin just before the try, but Wales still had only 13 players on the field when replacement forward Dewi Lake drove over in the 77th minute to tie the score at 29-29.
Wales captain Dan Biggar shaved the outside of the right post with his conversion attempt and gave away the critical penalty — an intentional knock down of a pass — to allow Willemse to save the Springboks in the third minute after the fulltime hooter.
"It was a shame they got away with it at the end, because I would have taken a draw," Wales coach Wayne Pivac said.
"It was a big step up from our last performance but the discipline is clearly disappointing. I'm very proud but very disappointed, it is one that got away."
"I'm gutted. There was a lot of momentum shifts in that game and we thought we nicked it but it wasn't to be," veteran Dan Lydiate said after his first Test in 17 months.
The second of three Tests will be played in Bloemfontein next Saturday with Wales still in search of a first win in South Africa after 11 attempts.