Wales were denied a first Test win in South Africa as the Springboks snatched a dramatic victory in an exhilarating second Test in Nelspruit.
Tries from Jamie Roberts and Alex Cuthbert put dominant Wales in control.
Ill-discipline, with Luke Charteris and Dan Biggar yellow-carded, allowed South Africa back into the game through a penalty try and a Cornal Hendricks try.
Ken Owens's try extended the lead but Willie Le Roux's effort and a converted penalty try gave the Boks victory.
Wales had not been behind in the game at the Mbombela Stadium until that moment, two minutes from time.
Referee Steve Walsh awarded a penalty try after Liam Williams shoulder-charged Hendricks into touch just short of the try-line, and Morne Steyn's conversion denied Wales only a second win over the Springboks.
Despite the defeat, it was a much-improved Welsh performance following the heavy first-Test defeat in Durban, one which had resulted in two changes to the XV.
The game started at an incredible pace, with the tourists making a promising start and earning a penalty after Jan Serfontein was penalised.
But Ospreys fly-half Biggar failed to secure the first points of the afternoon when he struck his 40-metre kick wide.
South Africa's first real venture into the Wales 22 came when wing Brian Habana led a counter attack, but Hendricks spilt his pass inside.
The Welsh were matching their hosts in a physical contest, and scored their first points after the Boks were punished for not rolling away at the ruck.
Biggar made no mistake with his second penalty, before Steyn missed a chance to bring his side level.
Wales were showing real intent, and their dominant opening 20 minutes was rewarded with a try following a well-executed move.
Cuthbert powered his way through the Springboks defence and offloaded to Racing Metro centre Roberts, who crashed under the posts despite the efforts of Serfontein.
Biggar added the conversion and, within two minutes, Wales had extended their lead.
After a spell of sustained pressure, Cardiff Blues wing Cuthbert crashed on to Biggar's pass and burst beyond Serfontein to score.
Biggar added the extras and, at 17-0, it was all going Wales' way. However, the game turned on its head in dramatic fashion when Charteris was sent to the sin-bin for stopping a riving maul illegally.
The Boks continued to press and Wales infringed once too often for referee Walsh's liking, the official awarding the home side a penalty try and yellow-carding Biggar.
With Wales down to 13 men, Steyn converted and South Africa took full advantage when Hendricks crossed following a move involving JP Pietersen and the impressive Le Roux.
Steyn converted and South Africa, having trailed 17-0 six minutes earlier, went into the break back in the game.
After surviving the late first-half onslaught, Wales took the game to their hosts at the start of the second half, and were rewarded with a third try.
Hooker Owens dived between Tendai Mtawarira and Duane Vermeulen to cross, with the try awarded following consultation between the referee and the TMO.
Biggar converted, and traded penalties with Steyn before the hosts were reduced to 14 men when Flip van der Merwe was yellow-carded for an aerial challenge on Wales captain Alun Wyn Jones.
A third successful penalty of the afternoon by Biggar put Wales 30-17 ahead and within touching distance of victory, but they were having to withstand a sustained period of Springboks pressure.
Camped on their own try-line, they defended heroically until Le Roux finally broke their resistance with a try converted by Steyn.
Then, in South Africa's final attack, Hendricks beat George North out wide before being clattered by an illegal tackle from full-back Williams just short of the line.
The Boks were awarded their second penalty try of the game, and Steyn's straightforward conversion from in front of the posts gave his side the lead for the first time.
Wales attempted to respond, but Biggar failed with two long-range drop-goal attempts as Wales once again fell just short against the Springboks.
South Africa: Willie le Roux; Cornal Hendricks, JP Pietersen, Jan Serfontein, Bryan Habana;Morné Steyn, Fourie du Preez; Tendai Mtawarira, Bismarck du Plessis, Jannie du Plessis, Flip van der Merwe, Victor Matfield (capt), Francois Louw, Willem Alberts, Duane Vermeulen.
Replacements: Schalk Brits, Gurthrö Steenkamp, Coenie Oosthuizen, Lood de Jager, Schalk Burger, Ruan Pienaar, Wynand Olivier, Lwazi Mvovo.
Wales: Liam Williams (Scarlets), Alex Cuthbert (Cardiff Blues), Jonathan Davies (Scarlets), Jamie Roberts (Racing Metro), George North (Northampton Saints), Dan Biggar (Ospreys), Mike Phillips (Racing Metro), Gethin Jenkins (Cardiff Blues), Ken Owens (Scarlets), Samson Lee (Scarlets), Luke Charteris (Perpignan), Alun Wyn Jones (Capt) (Ospreys), Dan Lydiate (Racing Metro), Josh Turnbull (Scarlets), Taulupe Faletau (Newport Gwent Dragons).
Replacements: Matthew Rees (Cardiff Blues), Paul James (Bath), Aaron Jarvis (Ospreys), Jake Ball (Scarlets), Dan Baker (Ospreys), Gareth Davies (Scarlets), James Hook (Perpignan), Matthew Morgan (Ospreys).
Referee: Steve Walsh