South Africa will go back to the top of the International Cricket Council Test rankings after some stubborn batting helped them to a hard-fought draw against Sri Lanka in Colombo.

Resuming on 38 for one, chasing a nominal 369, the tourists knew they only needed to bat out the final day of the second Test to seal a 1-0 series victory, which would be enough to displace Australia in the five-day rankings, and they just about held on.

Rangana Herath and Dilruwan Perera were a constant menace in spin-friendly conditions and understandably bowled in tandem for most of the day, with the former claiming five for 40, but some gritty batting from the Proteas helped them close on 159 for eight after 111 overs.

Captain Hashim Amla showed particular restraint on a day when occupying the crease was a higher priority than scoring runs, using up 159 balls for 25, while Vernon Philander's 27 not out off 98 deliveries saw South Africa home.

Any hopes of chasing an unlikely victory took a backseat when Perera beat Dean Elgar's uncertain defensive prod to bowl the opener after a rain delay.

Another shower shortly afterwards forced an early lunch but Sri Lanka made up for the lost time by bowling 47 overs in the afternoon session compared to the seven that were bowled in the morning.

On the resumption first-innings centurion Amla thumped Herath through the covers for four but he and Quinton de Kock were largely becalmed thereafter.

However, the latter perished for 37 off 92 balls when he inside-edged Herath onto his pad before being pouched at backward short-leg by Kithuruwan Vithanage.

That brought together Amla and AB de Villiers, South Africa's two most experienced batsmen and arguably their best players of spin, and for a time it appeared the two would see out the day.

Amla did survive a drop while on 13 off Herath but the batting duo put on 25 at a fraction more than a run an over before De Villiers was bowled by Herath after mis-reading the line to depart for 12 off 67 deliveries.

And Sri Lanka would have felt they were in with a real chance when they removed Amla and Faf du Plessis in the space of five overs after the tea interval.

The South Africa captain's stay was brought to an end when a loose drive off Perera only found Mahela Jayawardene, who claimed his 200th Test catch at slip when Herath found Du Plessis' outside edge.

In-between another delay for rain, JP Duminy, who made three off 58 balls in the first innings, hung around for 65 deliveries for the same score before he became the last recognised batsman to depart after being trapped in front by Perera.

A grandstand finish was guaranteed when Herath claimed his five-for after Dale Steyn was caught behind off a thin edge, but Philander repelled the slow left-armer and Perera in the closing stages to leave Sri Lanka cursing their fortunes.