Herath got rid of Quinton de Kock (37) and AB de Villiers (12) to restrict South Africa to 104-4 at tea. The visitors, who trail by 265 runs, need to bat a minimum 29 overs to avoid defeat.
The bowler, who had accounted for Alviro Petersen's wicket yesterday, returned impressive figures of 3-24 off 27 overs.
Skipper Hashim Amla (24 not out) though stood like a rock at one end, having occupied the crease for close to three hours on a deceptively turning wicket.
Even if they draw the game, South Africa will reclaim the world number one ranking from Australia.
Also Read
The second session saw the Proteas score just 49 runs from 47 overs while losing two wickets.
Amla, playing his first series since succeeding Graeme Smith as Test captain, had to call the physio twice for a bothersome hamstring but he dug in stoutly.
Herath would have picked the wicket of Amla as well but skipper Angelo Mathews grassed a low chance in the slips when the batsman was on 13.
Opener Dean Elgar was dismissed by off-spinner Dilruwan 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.