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.
The series victory propelled South Africa to the top of world Test ranking ahead of Australia and also ended its 21-year-old jinx on the island.
It was its first series win in Sri Lanka since 1993 when it 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 8 for 148 but tailenders Vernon Philander (27 not out) and Imran Tahir (4 not out) hung on in the final nine overs to finish on 8 for 159.
For Sri Lanka, it was only its 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 1 for 38, 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 five 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 for 60.
South Africa owed much to its 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 (3) 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 its second innings on 8 for 229.