The Springboks scored tries through Adrian Jacobs and Jean de Villiers, with Ruan Pienaar adding 10 points with the boot, as they began their three-match UK tour with a confidence-boosting win over the Six Nations Champions and Grand Slam winners.
With half-an-hour remaining in Cardiff, a Bok victory seemed a certainty but Wales hit back with four penalties from replacement fly-half James Hook to ensure the result remained in doubt until the final whistle.
The World Champions raced into a 10-lead with just nine minutes on the clock courtesy of Jacobs' sixth Test try and a conversion and penalty from the impressive Pienaar.
Pienaar's inclusion at outside-half had been the major talking point before the Boks left home at the end of last month and the Sharks pivot suggested he could make the position his own for the visit of the 2009 Lions with an accomplished display.
The 24-year-old has spent much of his career to date in the No9 shirt but he looked more than comfortable controlling the game from fly-half. As well as proving a constant threat when running at the Welsh defence, Pienaar kicked all of his four attempts at goal and supplied his outside backs with quick ball when required.
The Boks started where they left off against Australia in their last international back in August as they crossed for the game's opening score inside six minutes to silence the majority of the 74,119 crowd inside the Millennium Stadium.
Smooth handling from the South African forwards resulted in openside flanker Juan Smith being held up just short of the line but the Welsh defence was unable to repel the next Springbok attack as Jacobs crashed through. Quick ball allowed Fourie du Preez to feed the onrushing Sharks centre who timed his run perfectly to power underneath two Wales defenders on his way to the line.
Piennar converted from 10 metres to the left of the posts before adding his first three-pointer of the afternoon three minutes later after Wales had been penalised at a midfield ruck 37 metres from goal.
The remainder of the first period saw Wales debutant Leigh Halfpenny and Pienaar trade penalties but it was the visitors who took further control shortly after the break. Hook's first contribution since coming on as a 50th-minute replacement for Stephen Jones was to throw an interception pass to Jean de Villiers, with the Western Province man racing in unopposed from the halfway line. Again Pienaar converted to extend his team's advantage to 20-3 with 30 minutes left to play.
Although Wales dominated for the remaining half hour, South Africa had created enough of a platform to ensure Lions-bound Gatland suffered his first home defeat since taking over as Wales coach last year.
The Boks now travel to Edinburgh to take on Scotland at Murrayfield next Saturday, while Wales will be keen to get back to winning ways when they host Canada on Friday night.