Wales claimed only their second ever win over South Africa with a display of guts and cool control in Cardiff.
Leigh Halfpenny kicked all of Wales’ points, converting four of his five penalty kicks before going off injured.
South Africa were limited to two Pat Lambie penalties as Wales largely contained the Springboks attacking threat.
The tourists had a late five-metre scrum but the Wales pack disrupted the set-piece to seal a famous win.
Much of the build-up to the match featured Wales coach Warren Gatland fielding questions about whether he was under pressure because of losses this series to Australia and New Zealand, taking his record to just one win in 27 matches against the southern hemisphere big three.
This win, to add to Wales’ previous victory over South Africa in 1999 – also at the Millennium Stadium – provided a perfect riposte and kick-starts their build-up to next year’s World Cup in England.
Opposite number Heyneke Meyer had not been under quite the same scrutiny, although
in their opening tour game had raised some questions that were answered in subsequent wins over England and Italy.
Both sides were missing players because of injuries and recalls to clubs, as this match took place outside World Rugby’s official international window.
With George North concussed, Liam Williams stepped into the vacant Wales wing slot, given the opportunity to redeem himself after conceding the last-gasp penalty try that denied them victory in June’s second Test in Nelspruit.
Gatland had also suffered a run of injuries among his loose-head props, while Paul James had to depart back to Bath, but he could still call on British and Irish Lion Gethin Jenkins to fill the number one shirt.
Hooker Richard Hibbard had also been recalled to Gloucester, while South Africa had to change their entire back three with wing Bryan Habana and full-back Johan Goosen returning to France and JP Pietersen departing for Japan.
Wales full-back Halfpenny and Springboks fly-half Lambie exchanged early penalties, with Halfpenny unfortunate to hit the outside of the post with a later effort.
Space for the backs to work in was at a premium in a hotly contested first half, with both sides up quickly with some typically fierce defence.
Wales’ best moments came from chips ahead to turn the Springboks, the tactic enjoying particular success down the left wing.
Twice Williams came close to collecting the bouncing ball to plunge over the try line, but each time Lwazi Mvovo was there a split-second before, covering across to snuff out the danger.
South Africa almost broke the deadlock when Eben Etzebeth surged through the midfield, but he knocked on as the lock was scythed down by Halfpenny’s thumping tackle on the 22-yard line.
Lambie missed a long-range penalty that would have sent the Boks into the changing rooms with a narrow half-time lead, leaving the match delicately poised at the halfway stage.
Halfpenny and Lambie again exchanged early penalties after the restart, with the Springbok stand-off showing his range by slotting from the halfway line.
Wales enjoyed the best period of the game after that, keeping the ball in hand to pressurise the visitors into conceding two more penalties that Halfpenny converted.
Through all of this, the Wales half-back partnership of Rhys Webb and Dan Biggar shone, using possession wisely and keeping a level of control that South Africa found hard to wrest away.
Meyer used his bench early, making four changes including putting Handre Pollard on at fly-half in place of Lambie.
But the Springboks coach was forced into another change soon after because of a worrying injury to captain Jean de Villiers, who was carried off having dislocated his kneecap, and replaced by Damian de Allende.
South Africa suffered a further blow when Cornal Hendricks was sin-binned for taking out Halfpenny in the air.
The Wales 15 was clearly shaken by the collision and left the field soon after, with Scott Williams coming on and Liam Williams switching to his preferred full-back position.
Spurred on by their man advantage, Wales laid siege to the Springboks line but met furious resistance as first Jamie Roberts and then Scott Baldwin were stopped just short.
In the closing minutes, South Africa earned a five-metre scrum that threatened to undo all Wales’ hard work and deliver another gut-wrenching late loss to a southern hemisphere side.
But the Wales pack rose to the challenge, making a mess of South Africa’s platform and allowing the ball to be cleared upfield.
Wales kept their heads and kept the Springboks at bay for the final few moments to seal a well-deserved, and long overdue, win.
Halfpenny (Scott Williams 67), Cuthbert, Jonathan Davies, Roberts, Liam Williams, Biggar, Webb, Jenkins, Baldwin, Lee, Ball, Alun Wyn Jones, Lydiate, Warburton, Faletau.
Replacements (not used):
Emyr Phillips, Jarvis, Rhodri Jones, Charteris, King, Mike Phillips, Priestland.
Le Roux, Hendricks, Serfontein, De Villiers, Mvovo, Lambie (Hougaard 58), Reinach, Mtawarira (Nyakane 54), Bismarck du Plessis (Strauss 58), Oosthuizen (Redelinghuys 70), Etzebeth (De Jager 69), Matfield, Coetzee, Mohoje (Carr 54), Vermeulen.
Replacements (not used):
Pollard, De Allende.
John Lacey (Ireland).