Damian Willemse broke Welsh hearts with the final kick of a pulsating Test match as world champions South Africa triumphed 32-29 at Loftus Versfeld.
Willemse’s penalty – awarded following a deliberate knock-on by Wales captain Dan Biggar – denied Wales a draw.
And for large parts of a breath-taking contest, Wayne Pivac’s team looked destined to end 58 years of hurt by claiming a first victory over the Springboks in South Africa.
Beaten at home by Italy just three months ago, Wales were written off before they arrived in Pretoria, but wing Louis Rees-Zammit’s first-half try double helped put South Africa on the rack.
Wales, though, were briefly reduced to 12 men during the frantic closing stages after Rees-Zammit, Alun Wyn Jones and Rhys Carre received yellow cards.
A capacity 50,000 crowd – the Springboks’ first full house in South Africa since they won the World Cup 32 months ago – saw their heroes go 29-24 ahead through a 75th-minute penalty try, then replacement hooker Dewi Lake’s touchdown hauled Wales level before Willemse landed his winning kick.
Wales were humiliated 96-13 by South Africa on the same ground in 1998, but they head to Bloemfontein for next Saturday’s second Test after one of their finest performances in recent seasons.
South Africa scored tries through Bongi Mbonambi, Malcolm Marx and Cheslin Kolbe, in addition to the penalty try, while Willemse kicked two conversions and a penalty, and Elton Jantjies landed a penalty, with Biggar kicking three penalties, a drop-goal and a conversion.
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.