Hambantota - Tillakaratne Dilshan has inspired Sri Lanka to a consolation six wicket win in the third and final T20 against the Proteas in Hambantota on Tuesday to retain their number one ranking in the shortest format.
South Africa won the series 2-1, making amends for the 1-4 defeat in the preceding one-dayers on the tour.
But Tuesday's victory was crucial for Sri Lanka, who would have slipped to number three in the T20 rankings behind Pakistan and South Africa had they been whitewashed 3-0.
Dilshan brought up the thrilling win off the first ball of the 19th over, lofting Morné Morkel for his second six. He also hit nine boundaries.
Former captain Mahela Jayawardene hit a quickfire 33 in an opening stand of 67 off 35 balls with Dilshan to help Sri Lanka make a rollicking start.
The hosts were 123-4 in the 16th over when Thisara Perera (25 not out) put on a match-winning stand of 41 with Dilshan for the fifth wicket.
With 27 runs needed off the final 18 balls, the big-hitting Perera took 21 runs from the 18th over bowled by Wayne Parnell, including three fours and a six, to ease the pressure.
"It was not an easy target to chase, but the start I got with Mahela helped," said Dilshan, who was named man of the match.
"After that, I knew we could pass that total. It was important not to panic, but Thisara made the job easier.
"We may have lost the series, but it feels good to win this game."
South African captain Faf Du Plessis said the series win will boost his team ahead of next year's World Twenty20 in Bangladesh.
"It's a big trophy to take back on the plane," he said.
"We are really happy to win the series against the best team in the world. I thought 163 was enough, but one has to give credit to Dilshan.
"This series was a good experience for the boys because the conditions in Bangladesh will not be much different from here."
The hosts rested their two main players, batsman Kumar Sangakkara and fast bowler Lasith Malinga, and recalled Dilshan and Suranga Lakmal.
Earlier, du Plessis hit 85 off 65 balls and JP Duminy remained unbeaten on a 34-ball 51 as South Africa too put on a thrilling exhibition of power hitting.
Du Plessis, who won the toss for the third time in a row and elected to bat, smashed 10 boundaries and three sixes during a third-wicket stand of 112 with JP Duminy.
Du Plessis and Duminy put on their century stand after opener Henry Davids had been dismissed off the first ball of the match, leg-before to seamer Nuwan Kulasekara.
Duminy hit his 100th six in Twenty20 internationals when he lofted Kulasekara over the mid-wicket fence in the 17th over.
Du Plessis was dismissed in the final over, bowled by Lakmal.
The Man of the
Series award went to Duminy who made two half-centuries and also contributed
important wickets in the first match.
South Africa: 163-3 in 20 overs (Faf du Plessis 85, JP Duminy 51 not out)
Sri Lanka: 164-4 in 18.1 overs (T Dilshan 74 not out, M Jayawardene 33)
Sri Lanka: Dinesh Chandimal (captain, wicketkeeper), Kusal Perera, Mahela Jayawardene, Tillakaratne Dilshan, Angelo Mathews, Lahiru Thirimanne, Nuwan Kulasekara, Thisara Perera, Sachithra Senanayake, Ajantha Mendis, Suranga Lakmal
South Africa: Quinton de Kock (wicketkeeper), Henry Davids, Faf du Plessis AB de Villiers (captain), JP Duminy, David Miller, David Wiese, Wayne Parnell, Morne Morkel, Imran Tahir, Lonwabo Tsotsobe