Bath Rugby's Paul James suffered heartbreak in South Africa as a late, late penalty try denied Wales a first-ever away win over the Springboks.
A pulsating battle in Nelspruit ended 31-30 to the hosts, who were never ahead until Morne Steyn's simple conversion secured victory.
Bath flanker Francois Louw started for South Africa, while James replaced Gethin Jenkins at loosehead as Wales looked to protect their slender advantage in the closing stages.
Wales looked in total command as tries from Jamie Roberts and Alex Cuthbert helped them into a 17-0.
Yellow cards for Luke Charteris and Dan Biggar let the Springboks back into the contest and their first penalty try, together with a score from Cornal Hendricks, cut the gap to three points at half-time.
Hooker Ken Owens' try after the break and a conversion and two penalties from Biggar put the visitors 30-17 up and they defended superbly until Willie Le Roux replied with a converted score.
It put South Africa within striking distance and they finished the job when referee Steve Walsh awarded a penalty try after Liam Williams shoulder-charged Cornal Hendricks into touch just short of the tryine.
Steyn added the extras to leave Warren Gatland's men devastated at the end of a thrilling second Test.