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 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 tail-enders 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.