South Africa came from 18-3 behind to claim a 32-29 victory over Wales, but they were indebted to Damian Willemse who kicked a last-minute penalty to snatch the win.
Wales defended superbly in the opening exchanges and took their chances when they presented themselves.
Louis Rees-Zammit showed his searing speed to touch down twice while Dan Biggar kicked a conversion, a penalty and a drop-goal to give Wayne Pivac’s men an 18-3 lead at the break.
The Springboks’ response came through an Elton Jantjies three-pointer but he was replaced at the start of the second period and Willemse did a much better job in the fly-half position.
They were able to build pressure and eventually touched down twice via their trusty maul as Bongi Mbonambi and Malcolm Marx went over.
Two more Biggar three-pointers kept the hosts at arm’s length but the Boks were unrelenting and Cheslin Kolbe crossed the whitewash to move South Africa to within touching distance of the tourists.
A penalty try – once again via their lineout drive – took the home side into the lead and led to Wales going down to 12 men as Rhys Carre followed Alun Wyn Jones and Rees-Zammit into the bin.
Jones returned to help the visitors level matters when Dewi Lake scored, but there was late heartbreak for Pivac’s charges when a deliberate Biggar knock-on resulted in Willemse kicking the winning points.
A minute’s silence was held before kick-off in memory of former Wales and British and Irish Lions captain Phil Bennett, who died last month, while the Wales players wore black armbands.
And Wales scored a try after just three minutes that would have thrilled Bennett, with a flowing move ending in Liam Williams sending wing Rees-Zammit clear, and the Gloucester speedster cruised home from 35 metres out.
Biggar underlined the quality of his team’s start by landing a 40-metre drop-goal that left South Africa eight points adrift inside the first 10 minutes.
The Springboks needed a response, but a long-range Jantjies penalty hit one post and then struck the other before bouncing to safety from a Welsh perspective.
Jantjies made no mistake off the tee three minutes later, though, before Biggar cancelled out that penalty and the opening quarter ended with Wales 11-3 ahead.
Jantjies then missed another penalty, and he also struggled tactically, but his sharp break put Wales under pressure, only for the visitors to claim a second try eight minutes before half-time.
Centre Nick Tompkins’ challenge on Jantjies saw the ball bounce clear, and Rees-Zammit kicked ahead before gathering possession and surging clear for his second try.
Biggar’s conversion made it 18-3, and although the skipper was then yellow-carded for slowing the ball down after a try-saving tackle on Springboks scrum-half Faf de Klerk, South Africa had a mountain to climb, trailing by 15 points at the break.
South Africa had to start eating into Wales’ lead, and they claimed their first try after 46 minutes when Mbonambi was driven over by his fellow forwards.
Full-back Willemse added the conversion after moving to fly-half instead of a substituted Jantjies, before Biggar rejoined the action.
South Africa, helped by their enviable strength off the replacements’ bench, had Wales on the back foot, while the visitors saw substitute prop Tomas Francis taken off after suffering what appeared to be a nasty injury.
And before Wales could regroup, the Springboks struck again, as replacement hooker Marx touched down, making it 18-15.
Biggar kicked two penalties during a six-minute spell either side of the hour mark, which meant Wales went into the final quarter with a nine-point advantage.
The Springboks hit back, though, through a try for wing Kolbe that Willemse converted from the touchline, before Jones was yellow-carded for being offside in the build-up to Kolbe’s score.
Rees-Zammit was also sin-binned following a technical infringement, reducing Wales to 13 players, and South Africa went ahead with a penalty try after Wales collapsed a maul, before Carre became their third player in the sin-bin at the same time.
Incredibly with three minutes left, Wales drew level through Lake’s try, but Biggar was then guilty of a deliberate knock-on and Willemse kicked the match-winner.