South Africa reclaimed top spot in cricket's test rankings after grimly hanging on for a draw in the second and final test against Sri Lanka overnight to win the series 1-0.
It was South Africa's first series win under new captain Hashim Amla and only their second series victory in Sri Lanka in 21 years, and it propelled them past Australia as the world's No 1 team.
''Getting to number one wasn't our primary aim. We wanted to find a winning formula with a new team,'' an elated Amla said after the match.
''After the first test which we did manage to win and the character we showed here shows we are heading in the right direction and have a good formula in the years to come.''
Set 369 for victory, South Africa, who started the day at 38-1, finished on 159 for eight with Vernon Philander holding firm with an unbeaten 27.
That was despite a marathon bowling effort from spinners Rangana Herath and Dilruwan Perera who sent down 89 overs between them, including 54 maidens, to claim all eight South African wickets.
''To win a series here you have to encounter certain times where you would have to reach a breaking point,'' added Amla.
''We almost reached that but the guys hung in there and we managed to come out with a draw.
''It's a very pleasing thing especially my first as captain I certainly enjoyed winning here in Sri Lanka, not many foreign teams come and do it and for us to have done it's a thank you to the team for the commitment they showed.''
Sri Lanka had to fight not only against the strong defence put up by South Africa but also the weather that interrupted play three times during the day, including in the final session with South Africa on 118-6.
However, the sun came out strongly and Sri Lanka grabbed the wicket of JP Duminy in the first over of the final hour when Perera trapped him lbw for three.
Herath picked up his 18th five wicket bag when he had Dale Steyn caught behind by Niroshan Dickwella for six.
There was no further joy for the hosts, however, as Philander and Imran Tahir played out the remaining 27 minutes.
Amla had led South Africa's resistance when he scored a fighting 25 off 159 balls before falling to Perera in the fourth over after tea when he edged an attempted drive to Mahela Jayawardene.
Herath also found the outside edge of Faf du Plessis' bat on 10 to give 37-year-old Jayawardene, who retires next month after the series against Pakistan, his 200th test catch.
Herath had also dismissed Quinton de Kock for 37 and AB de Villiers for 12 in the afternoon session, but the rain delays, which saw only 30 minutes play in the morning session, hindered Sri Lanka's push to level the series.
South Africa had lost the number one ranking to Australia in May.
''When you play the number two ranked team in the world, you have to be on top of your game,'' said Sri Lanka captain Angelo Mathews.
''Right from the start I said we need to sharpen our game to win against them. Simply because we play at home and we know the conditions they are not going to lose a game of cricket to us,'' he said.
Jayawardene won the man of the match award and Dale Steyn the man of the series.
Should bouncers be banned from cricket?