Needing 351 runs at the start of the fourth day after Sri Lanka had set them 490 to win, South Africa were bowled out for 290 early in the second session on Monday, 23 July. This despite a maiden Test century by Theunis de Bruyn (101) and a half-century by Temba Bavuma (63).
South Africa had managed just 124 runs in their first innings in reply to Sri Lanka's 338 and the hosts had declared their second innings on 275/5.
De Bruyn and Bavuma dominated the Sri Lankan bowling for most parts of the first session, adding 123 runs for the fifth wicket before Bavuma was caught behind off a Herath delivery that spun away sharply from the batsman and got his outside edge.
De Bruyn brought up his maiden Test half-century off 118 balls, while Bavuma brought up his own off 69 balls.
Together, they negated the Sri Lanka spinners and even made Suranga Lakmal bring himself into the attack for the first time in the Test match.
Bavuma also survived when Sri Lanka reviewed the not out lbw decision made by the on-field umpire off the bowling of Dilruwan Perera. De Bruyn, at the other end, looked mostly comfortable during his stay, using the depth of the crease to full effect and sweeping and reverse-sweeping the Sri Lankan spinners.
They brought up their 50-run partnership off 87 balls and the 100-run partnership off 182 balls and took the team past 150 and 200 in the process. But it was only a matter of time before Sri Lanka broke through.
Bavuma's wicket led to another as Herath trapped Quinton de Kock lbw right at the stroke of lunch to reduce South Africa to 246/7 at the break. De Kock reviewed the decision against him, but the DRS returned 'umpire's call'.
Once the batsmen were dismissed, it was easy for the Sri Lankan spinners to run through the tail as Kagiso Rabada, Dale Steyn and Lungi Ngidi could not offer much resistance. But this was not before de Bruyn got his maiden Test hundred off 228 balls, with a paddle sweep off Herath.
He became only the second South African after Jonty Rhodes in 1993 to make a fourth-innings century in Asia.
However, he was dismissed soon after he got to the landmark when Herath got one to go on straight and take out the off-stump as de Bruyn shouldered arms, perhaps expecting the ball to turn.
Rabada went immediately after, caught at first slip off Perera and Steyn was the last man to fall, also to Herath. Dilruwan Perera and Akila Dananjaya finished with two wickets each.