Malcolm Marx scored the only try of the match as world champions South Africa ended an eight-year wait for a win over Wales in Cardiff with a 23-18 victory on Saturday.
Wales were 18-15 ahead when hooker Marx was driven over from a close-range line-out with seven minutes left at a rainswept Principality Stadium.
Replacement Elton Jantjies then rounded off the win with a last-minute penalty.
Wales had threatened a try in the 65th minute only for replacement Liam Williams, looking to utilise a possible overlap, to find his path blocked by a pitch invader.
All of Wales' points came via six Dan Biggar penalties.
Opposing fly-half Handre Pollard kicked four penalties, with replacement Frans Steyn also landing a long-range effort for the Springboks in front of more than 70,000 spectators.
"What a special day in a packed stadium like this, thank you to everybody that came out," South Africa captain Siya Kolisi told Amazon Prime after the Springboks' first win in five matches against Wales in Cardiff.
The teams' last meeting saw South Africa edge Wales 19-16 in a World Cup semi-final in Japan two years ago, with Kolisi adding: "Wales is a very strong team...We knew Wales would start well."
- 'On the edge' -
Wales coach Wayne Pivac added: "It was a massive effort from our boys and we're obviously disappointed not to hold on in the last 10 minutes.
"We lived on the edge and we gave away a couple of penalties. One right on half-time which I thought we didn't need to give away and then one towards the end which gave them that line-out drive."
Wales made six changes to the side thrashed 54-16 by New Zealand in their end-of-year opener in Cardiff last weekend with flanker Ellis Jenkins making his first Test appearance in three years since suffering a knee injury during a 2018 win over the Springboks.
Meanwhile, Will Rowlands replaced Wales captain Alun Wyn Jones after the veteran lock suffered a shoulder injury against New Zealand, with centre Jonathan Davies taking over as skipper.
South Africa made three changes to the the side that beat New Zealand 31-29 in the Rugby Championship last month, with Damian Willemse at full-back, Jesse Kriel on the right wing and Herschel Jantjies at scrum-half in place of injured World Cup winner Faf de Klerk.
With the stadium's retractable roof left open to combat the spread of Covid-19 and the rain lashing down, it was no surprise that both sides deployed the high kick.
But Biggar's long pass almost sent in Louis Rees-Zammit at the right corner only for flanker Kolisi to make a superb try-saving tackle.
Biggar's 10th-minute penalty opened the scoring but Pollard equalised soon afterwards.
The two No 10s exchanged penalties either side of Willemse leaving the field for a head injury assessment.
Biggar landed a third penalty and was on target again after South Africa prop Ox Nche was yellow-carded for obstructing Nick Tompkins.
Wales too were a man down before half-time, prop Rhys Carre penalised for entering a maul from the side, before Pollard's penalty on the stroke of the interval left the hosts 12-9 up.
Early in the second half, South Africa deployed their 'bomb squad', with the replacement front row of Marx, Steven Kitshoff and Vincent Koch looking to maintain the Springboks' set-piece dominance.
Biggar extended Wales' lead with a penalty from half-way only for the impressive Steyn to surpass that effort with a 54-metre goal.
South Africa's pack forced a scrum penalty and Pollard tied the scores at 15-15 heading into the last quarter.
But when it seemed Wales might score the game's first try, a pitch invader blocked Williams.
Mark Williams, Principality Stadium Manager said afterwards: "We condemn this type of behaviour in the strongest possible terms. He (the invader) was intercepted and immediately escorted from the stadium and handed over to South Wales Police."
Biggar's sixth penalty nudged Wales 18-15 ahead.
Springbok wing Makazole Mapimpi looked to have scored a try, but it was disallowed for offside, not that it mattered in the end.
Wales are at home to Fiji ne/djxt weekend, when South Africa face Scotland in Edinburgh.