It was the Springboks’ first win in Cardiff since 2013, but they did not secure it until substitute hooker Malcolm Marx scored a 73rd-minute try and Elton Jantjies kicked a penalty.
Despite being without injured captain Alun Wyn Jones and a host of other absent British and Irish Lions, Wales delivered a towering performance, with fly-half Dan Bigger kicking six penalties.
Biggar’s sixth successful penalty put Wales back in front at 18-15, although there was drama just a minute earlier when the hosts attacked through Liam Williams just as a spectator ran on to the pitch only a few metres away from him.
Williams had to jump over the pitch invader before he was duly led away, although Wales would not have scored.
Handre Pollard kicked four penalties and Frans Steyn one for South Africa, yet Wales bounced back superbly from a 54-16 drubbing against New Zealand last weekend.
And their outstanding performer was flanker Ellis Jenkins who suffered a major knee injury against the Springboks three years ago and had not played Test rugby since.
Wales were without Jones, while other unavailable Lions included Ross Moriarty, Ken Owens, Justin Tipuric and George North, with centre Jonathan Davies taking over as skipper.
South Africa made three switches from the side that beat New Zealand last month, with full-back Damian Willemse, wing Jesse Kriel and scrum-half Herschel Jantjies all featuring behind an unchanged pack.
Wales made a bright start and shaded the early possession, with Biggar mixing his running and kicking game impressively before wing Louis Rees-Zammit was denied a try by brilliant Springboks cover defence.
Biggar kicked Wales into the lead through a 10th-minute penalty, but Pollard landed an equalising strike two minutes later as persistent drizzle swirled around the stadium.
Biggar restored Wales’ advantage through a second penalty, while South Africa saw Willemse go off for a head injury assessment as Steyn joined the action.
A tight opening quarter reached its conclusion with South Africa showing an early sign of scrum dominance and Pollard kicking a second penalty to make it 6-6.
South Africa attempted to launch their main ball-carriers at Wales, but the home side defended superbly, with Jenkins at the forefront.
Biggar completed his penalty hat-trick 13 minutes before the break, and referee Paul Williams’ growing impatience at South Africa’s poor technical discipline saw him engage in a lengthy conversation with Springboks skipper Siya Kolisi.
South Africa then saw prop Ox Nche yellow-carded for obstruction on Tompkins, and Biggar’s fourth successful penalty opened up a six-point advantage.
But even though Wales had prop Rhys Carre sin-binned for a technical infringement, they deservedly claimed a 12-9 interval lead after Pollard landed a third penalty.
South Africa sent on a replacement front row of Steven Kitshoff, Marx and Vincent Koch just five minutes after the restart as they looked to establish a foothold in the game.
Wales were forced into a defensive rearguard, but they tackled and harried magnificently before breaking away, forcing South Africa to infringe, and Biggar kicked a 45-metre penalty.
It was a stirring effort by the home side, and although the Springboks looked more threatening in attack than during the first half, it required a 52-metre penalty from Steyn for their next points.
South Africa, however, had other ideas, breaking into Wales’ half from 70 metres out, but wing Makazole Mapimpi had a try disallowed, and the home side retained their narrow advantage before Marx struck following a relentlessly-driven lineout, but Elton Jantjies missed the conversion.
Jantjies final kick penalty, though, meant South Africa sealed the deal and finally broke Welsh resistance.
Register for free to continue reading
Registration is a free and easy way to support our truly independent journalism
By registering, you will also enjoy limited access to Premium articles, exclusive newsletters, commenting, and virtual events with our leading journalists
Already have an account? sign in