A 77th-minute penalty try saw South Africa come from behind to edge Wales 31-30 in a drama-filled encounter in Nelspruit on Saturday.
Wales looked on course to record their first-ever win on South African soil in 50 years of asking before a shoulder-charge from Liam Williams on Cornal Hendricks saw the Boks awarded their second penalty try of the game.
In a stunning turnaround in fortunes after last week's drubbing in Durban, Warren Gatland's side led for almost the entire game, matching their hosts physically.
Wales led 17-14 at half time after dominating the opening exchanges.
Tries from Jamie Roberts and Alex Cuthbert saw the visitors race to a 17-0 lead before two yellow cards in the space of three minutes allowed South Africa to come storming back.
The Boks employed their rolling maul to devastating effect, earning a penalty try and forcing two Welshmen into the sin bin as the visitors tried to stop the rumbling green machine.
Hendricks added his name to the scorecard before the interval as the Welsh lead vanished as quickly as it was gained.
Wales moved ahead again as a Ken Owens try early in the second period and a couple of Dan Biggar penalties put visitors in pole position before a piece of Willie le Roux magic set up a grandstand finish.
Wales came out of the blocks breathing fire and punished the error-strewn Boks. Biggar got the scoreboard ticking with a penalty for a ruck infringement after 12 minutes.
It was all Wales in the first quarter, and Roberts was first over the whitewash after Cuthbert burst through a gap left by Fourie du Preez before offloading to send the big centre under the posts.
Biggar's conversion made it 10-0 as the home side looked shell-shocked.
There was more to come from Wales' strong-running backs as Cuthbert was next to crash over after he and Liam Williams had put their team on the front foot.
The half-hour mark saw the Boks revert to their bread and butter and Wales had no reply to the rolling maul. Luke Charteris saw yellow for collapsing one before the next drive ended with referee Steve Walsh raising his arm under the sticks for the hosts' first points. Biggar saw yellow too.
Morne Steyn slotted the conversion and spread the ball wide as soon as it reached his hands again. The two-man numerical disadvantage was too much for Wales to handle as JP Pietersen bust down the left touchline and linked with Willie le Roux, who sent it to the opposite side of the field and the hands of the racing Hendricks, who bolted home.
Steyn's second conversion meant it was a three-point ballgame as the half-time hooter sounded.
The visitors avoided further damage and struck again as soon as they were back to 15 men. There was more than a hint of a knock-on as Owens stretched out an arm towards the tryline but Walsh was satisfied he had maintained control.
Biggar's conversion put the men in red 10 points ahead.
South Africa squandered a scoring chance when Du Preez took a very kickable penalty quickly only to see his forwards make a mess of it. Matfield made sure Steyn slotted the next one to make it 24-17.
Biggar could restore the 10-point gap immediately though as the Boks infringed at the restart.
The situation got worse for the hosts just before the hour mark as Flip van der Merwe saw yellow for taking Alun Wyn Jones out in the air.
Biggar extended the Welsh lead with his third penalty after the Bok scrum faltered, meaning the tourists led by 13 point with 15 minutes to play.
Le Roux came to the rescue, selling a dummy and accelerating into a gap to score.
The finale couldn't have been tighter as Hendricks shrugged off a tackle from George North before diving for the line and being bumped out by Williams.
Walsh had a massive call to make, but got it right, allowing Steyn to slot the winning conversion.
Biggar tried to snatch the win with two long-range drop goals, but fell well short.
Man of the match: Plenty of names to mention including home town favourite Duane Vermeulen and Wales prop Gethin Jenkins. But we'll go for Alex Cuthbert, who was unstoppable in the first half.
Moment of the match: Only one option here, but Walsh's decision to award South Africa's second penalty try was correct.
Villain of the match: No nasty stuff to report.
For South Africa:
Tries: Penalty try 2, Hendricks, Le Roux
Cons: Steyn 4
Yellow card: Van der Merwe
Tries: Roberts, Cuthbert, Owens
Cons: Biggar 3
Pens: Biggar 3
Yellow cards: Charteris, Biggar
South Africa: 15 Willie le Roux, 14 Cornal Hendricks, 13 JP Pietersen, 12 Jan Serfontein, 11 Bryan Habana, 10 MornÃ© Steyn, 9 Fourie du Preez, 8 Duane Vermeulen, 7 Willem Alberts, 6 Francois Louw, 5 Victor Matfield (c), 4 Flip van der Merwe, 3 Jannie du Plessis, 2 Bismarck du Plessis, 1 Tendai Mtawarira.
Replacements: 16 Schalk Brits, 17 Gurthro Steenkamp, 18 Coenie Oosthuizen, 19 Lood de Jager, 20 Schalk Burger, 21 Ruan Pienaar, 22 Wynand Olivier, 23 Lwazi Mvovo.
Wales: 15 Liam Williams, 14 Alex Cuthbert, 13 Jonathan Davies, 12 Jamie Roberts, 11 George North, 10 Dan Biggar, 9 Mike Phillips, 8 Taulupe Faletau, 7 Josh Turnbull, 6 Dan Lydiate, 5 Alun Wyn Jones (c), 4 Luke Charteris, 3 Samson Lee, 2 Ken Owens, 1 Gethin Jenkins.
Replacements: 16 Matthew Rees, 17 Paul James, 18 Aaron Jarvis, 19 Jake Ball, 20 Dan Baker, 21 Gareth Davies, 22 James Hook, 23 Matthew Morgan
Venue: Mbombela Stadium, Nelspruit
Referee: Steve Walsh (Australia)
Assistant referees: Romain Poite (France), Francesco Pastrana (Argentina)
Television match official: Glenn Newman (New Zealand)
Assessor: Lyndon Bray (New Zealand)