Electing to bat, Sri Lanka posted a challenging 165 for seven in 20 overs, riding on a 40-ball 61 by Perera and then restricted South Africa to 160 for eight, thanks to some fine bowling towards the end.
South Africa seemed to be cruising towards the target of 166 as they required 19 runs from the last two overs, but Sri Lankan pacers Nuwan Kulasekara and Lasith Malinga choked the Protea batsmen with their superb bowling.
South Africa's run chase was built on a 60-run second-wicket stand between opener Hashim Amla (23) and top-scorer J P Duminy (39) to find themselves in a strong position at 75 for one at the halfway mark only to fritter away the advantage in the next 10 overs.
For Sri Lanka, off-spinner Sachithra Senanayake grabbed two wickets for 22 runs while Kulasekara, Angelo Mathews, Malinga and Ajantha Mendis chipped in with a wicket apiece.