Fourteen-man Ireland beat the Springboks on South African soil for the first time in front of 42,620 stunned fans at Newlands.
The tourists lost CJ Stander to a red card midway through the first half, but they kept playing positive rugby and Paddy Jackson kicked them home.
South Africa had not played since last year's World Cup and were in their first game under new coach Allister Coetzee, but Ireland had their own problems with Johnny Sexton leading a long list of injured players who missed the tour.
Ireland started the game well, taking advantage of South Africa's indiscipline to enjoy some good field position and their maul was functioning well.
Although Pat Lambie missed an early penalty attempt to put the Springboks in front, the traffic was largely one-way and it was the tourists who drew first blood when referee Mathieu Raynal punished the hosts for pulling down a lineout drive.
Rory Best went to the corner and, again, the maul came down. Iain Henderson carried the advantage at the home side, before Luke Marshall deftly chipped the ball in behind for Jared Payne to dot down. It was to be a double-blow for the 'Boks who lost Lood de Jager to the sin-bin for the maul offence as Jackson converted the try.
Things turned in the South Africans' direction on 22 minutes, however, as Ireland's early good work was undone by a red card for Stander.
On his return to his homeland, the Munster back-row connected with his former Junior Springbok team-mate Lambie's head with his hip after the fly-half had kicked the ball. Raynal reviewed the incident several times, before concluding that the 26-year-old had been reckless and reached for his pocket.
It was Ireland's first red card since Jamie Heaslip was sent off in similar circumstances in New Zealand in 2010.
Despite losing their No.10, the home side scented blood and Elton Jantjies narrowed the gap to four with a penalty before a piece of deft skill saw the Lions fly-half put Lwazi Mvovo over.
And things got worse for Ireland as the referee reviewed the footage and found that Robbie Henshaw had gone high and late on Jantjies after the pass, so he was handed 10 minutes in the sin-bin as the conversion sailed over.
Despite playing the rest of the half with 13 men, Ireland somehow managed to go in level as Jackson landed an opportunistic drop-goal before Conor Murray brilliantly denied a Duane Vermeulen charge and Jackson forced Mvovo into touch.
Remarkably, they followed it up with a try to take the lead three minutes after half-time to a reaction of stunned silence at Newlands.
Slick hands from Payne saw Andrew Trimble make gains up the right, his inside pass was touched by Willie le Roux and captain Rory Best pounced with a big carry. Murray picked and went, slipping de Jager's tackle to put his side in front.
Jackson converted and almost added another try when he slipped inside Frans Malherbe and chipped ahead, but Faf de Klerk was too quick for him and averted the danger.
South Africa came on strong, but Ireland's defence held with captain Best leading from the front with a couple of crucial interventions.
Indeed, Jackson came close to making it a two-score game with a long-range penalty, but his effort came back off the post.
He made no mistake with his next effort from closer range, but South Africa hit back instantly when Pieter-Steph du Toit intercepted Jackson's pass and raced in under the posts.
Jantjies converted to make it a three-score game, but Ireland kept coming and Jackson extended the lead to six from the kicking tee after missing a drop-goal attempt.
They had to play all the way to the end as the Springboks threw everything at them.
JP Pietersen looked like he might have scored with the clock having ticked past 80, but a combination of Payne, Henshaw and Jackson forced him into touch.