World champions South Africa required an 83rd-minute penalty from Damian Willemse to defeat an indisciplined Wales side 32-29 in Pretoria.
The Springboks' first home match in front of a crowd since winning the 2019 Rugby World Cup was an epic – albeit not the sort the South Africa fans might have anticipated.
Despite four yellow cards that saw Wales squander a half-time advantage, Wayne Pivac's men almost rescued a remarkable result, only to be denied at the last.
Two Louis Rees-Zammit tries gave Wales a commanding lead, but Dan Biggar headed to the sin bin shortly before the interval, and Bongi Mbonambi powered over following the restart.
Malcolm Marx also crossed, and the visitors appeared to have collapsed after Cheslin Kolbe's try was followed by a yellow card for Alun Wyn Jones – the awarding of which caused some confusion.
Rees-Zammit went from hero to zero as his desperate attempt to stop a South Africa try saw him join Jones on the sidelines, and Wales were down to 12 men and behind when Rhys Carre's intervention resulted in a penalty try.
What a game! The Boks snatch the victory at the death courtesy of a pressure penalty goal by Damian Willemse. It ends 32-29 to South Africa after 80 minutes of very tough Test rugby. Thanks for anothe epic, @WelshRugbyUnion!#StrongerTogether #StrongerForever #SAvWAL pic.twitter.com/U1aDlLNyT6— Springboks (@Springboks) July 2, 2022
But the tourists did not lack spirit and sensationally drew level again through Dewi Lake with 13 on the pitch, only for Biggar to marginally miss the posts with the conversion when he could have secured a late lead.
Instead, Biggar's knock-on at the other end allowed Willemse to kick the Springboks to a memorable triumph to begin this three-Test series, which continues in Bloemfontein next Saturday.