Sachithra Senanayake struck a six off the penultimate ball to give Sri Lanka a two-wicket triumph over South Africa on Sunday in the final match of a five one-day international series won 3-2 by the hosts.
Captain AB de Villiers and predecessor Graeme Smith (125 each) hit tons as South Africa made 312-4, but a century from Kumar Sangakkara (102) led the tourists to 314-8 at the Wanderers and a second success within three days.
Sri Lanka appeared to be cruising until Sangakkara became the fourth wicket to fall with 275 runs on the board 44.2 overs into the match and a late South Africa rally brought two wickets within three balls during the final over.
It left the tourists needing five runs from two balls for victory and tailender Senanayake hit Robin Peterson for a six to snatch success in a thrilling climax to a two-month tour.
"We came back strongly at the end of our tour and needed to prove a point after being bowled out for 43 in the first ODI," said Sri Lanka skipper Tillakaratne Dilshan.
"Chasing 313 runs for victory meant it was always going to be tight and I am so proud of my team. We had a fantastic time in South Africa and it was just a pity that we lost the Test and one-day series narrowly."
Man-of-the-series De Villiers said: "We have a lot to work on before touring New Zealand next month, but I am very happy with the way the team fought to the end."
Sangakkara struck 10 fours in a maiden ODI ton against South Africa and there were also valuable contributions from Lahiru Thirimanne (69), Upul Tharanga (46) and Dilshan (41).
De Villiers and Smith excelled as South Africa recovered from a sluggish early run rate having being sent in to bat in a match delayed for one hour by a mid-afternoon thunderstorm.
De Villiers finished unbeaten after facing 98 deliveries while Smith received a standing ovation from the sell-out 30,000 crowd after being caught by Dinesh Chandimal at mid-wicket off the bowling of Lasith Malinga.
While the Proteas skipper confirmed his position as one of the best stroke players in the world today with 10 fours and four sixes, Smith could not contain his joy as he walked off after a timely first ODI ton in three years.
There was a media and public outcry against him just a week ago after innings of six, 28 and two in the first three matches of the series before he stopped the rot with a brisk 68 in Kimberley two days ago.
Test skipper Smith embroidered his innings with nine fours and four sixes off 143 balls during a 203-minute stand and the crowd revelled in his change of fortune.
South Africa lost opener Alviro Petersen (6) with only 10 runs on the board and were 70-2 when Faf du Plessis (24) departed before De Villiers and Smith took charge.