A gritty South African tail fought off Sri Lanka's spinners to snatch a nerve-jangling draw in the second Test and seal a historic 1-0 series win. (Scorecard)
The series victory propelled South Africa to the top of world Test ranking ahead of Australia and also ended their 21-year-old jinx on the island.
It was their first series win in Sri Lanka since 1993 when they first toured the country and defeated the hosts 1-0. South Africa had won the opening Test in Galle by 153 runs.
Chasing a target of 369, the visitors were reeling at 148-8 but tailenders Vernon Philander (27 not out) and Imran Tahir (four not out) hung on in the final nine overs to finish on 159-8.
For Sri Lanka, it was only their second home series defeat since 2006.
The South Africans were booed by the small but noisy crowd at the SSC ground after Tahir indulged in some time wasting tactics towards end of play.
Starting the day at 38-1, South Africa added just 121 runs off 94 overs, opting for defensive batting rather than press for a win.
To their credit, the Sri Lankan spinners gave it their all but constant rain breaks and sloppy fielding put paid to their efforts.
Left-arm spinner Rangana Herath led the pack with 5 wickets from 45 overs while giving away just 40 runs. The stocky 36-year-old was well supported by off-spinner Dilruwan Perera who grabbed 3-60.
South Africa owed much to their new Test skipper Hashim Amla who occupied the crease for close to three hours despite a troublesome hamstring, making 25 off 159 balls in a remarkable show of resistance.
Amla, playing his first series since succeeding Graeme Smith as Test captain, at one stage faced 57 successive dot balls, frustrating the Sri Lankan bowlers no end.
He was finally dismissed by Perera who had him caught by Mahela Jayawardene in the fourth over of the final session.
Jayawardene then caught Faf du Plessis (10) for his 200th catch at slip to hand Herath his fourth wicket of the innings.
Jean-Paul Duminy (three) fended off the spinners for 65 balls before being trapped leg before off Perera.
Herath had Dale Steyn caught behind with just 20 minutes left for close of play, raising hopes of a Sri Lanka win.
The second session saw Herath uproot the off-stump of AB De Villiers (12) after having Quinton de Kock (37) caught by Kithuruwan Vithanage at backward short leg.
Opener Dean Elgar was dismissed by Perera in the truncated start-stop first session that saw the visitors add 17 runs off seven overs.
Elgar made 13 off 65 balls and shared 43 runs for the second wicket with De Kock.
The hosts, who posted 421 in their first essay, had bowled out South Africa for 282 in their first innings to gain a 139-run lead.
Sri Lanka declared their second innings on 229-8.