AB de Villiers scored a brilliant half-century on his South Africa comeback, but Niroshan Dickwella and Seekkuge Prasanna made their highest Twenty20 international scores to seal a five-wicket win and 2-1 series victory for Sri Lanka.
The imperious De Villiers had not featured for the Proteas since last June after undergoing shoulder surgery and made up for lost time by top scoring with 63 from only 44 balls in a total of 169-5 at Newlands on Wednesday.
Imran Tahir claimed outstanding figures of 3-18 in Sri Lanka's reply, but opener Dickwella (68 off 51) and all-rounder Prasanna (37 not out from 16) starred as the tourists got home with one ball to spare to complete the fightback after losing the first game of the series.
De Villiers made a century on his return for Northerns last weekend and hit the ground running back on the international stage, treating an expectant crowd to some ingenious strokes in a stand of 71 with Reeza Hendricks (41 from 34).
Mangaliso Mosehle blasted a quickfire unbeaten 32 to leave Sri Lanka, led by Dinesh Chandimal after Angelo Mathews returned home, with a victory target of 170.
The outstanding Dickwella came up with some inventive shots to keep the tourists on course and three dropped catches in as many balls from a Wayne Parnell over gave them a helping hand.
Sri Lanka were 118-4 when the superb Tahir took two wickets for as many runs in the 16th over to make South Africa favourites, but Prasanna hit three sixes in as many balls and Asela Gunaratne struck the winnings runs off the final over from debutant Dane Paterson.
Lungi Ngidi was only able to bowl two overs for the Proteas after suffering what appeared to be a side injury just three days before the one-day international series gets under way.