Sri Lanka is eyeing a big victory as South Africa, needing 490 to win the second cricket Test, was reduced to 139 for five in its second innings at stumps on the third day on Sunday.
Spinners Akila Dananjaya and Rangana Herath took two wickets each to place the hosts within five wickets of a 2-0 series sweep.
Theunis de Bruyn was batting on 45 with Temba Bavuma on 14.
Herath produced the first breakthrough for Sri Lanka trapping Aiden Markram lbw for 14 with the total on 23.
Dean Elgar and de Bruyn shared 57 runs for the second wicket, aided by a reprieve for Elgar from a catch by the wicketkeeper off a Dilruwan Perera no ball. Elgar failed to make his luck count as he was trapped lbw by Perera for 37 with the total on 80.
Dananjaya returned two for 35 while Herath had two for 54.
Earlier, Sri Lanka declared its second innings on 275 for five setting South Africa a challenging target.
Dimuth Karunaratne top scored for Sri Lanka with 85 while Angelo Mathews (71) and Danushka Gunathilaka made useful contributions.
South African left-arm spinner Maharaj took three wickets for 154 runs following up on his career-best nine wickets in the first innings.
South Africa is 0-1 down in the two-match series and must put on a record breaking performance on a spinning pitch at the Sinhalese Sports Club if they are to level the series.
"It is going to be a huge achievement for us as we are an inexperienced side," Mathews said of the potential series win.
"Beating the No. 2 team in the world is something special...but we have a job at hand tomorrow morning. If we can knock off a couple early, then we can enjoy the victory."
The highest successful run chase in Test cricket is 418 made by the West Indies in Antigua in 2003.
South Africa have not lost a Test series to Sri Lanka since 2006.
South Africa wicketkeeper Quinton De Kock became the fastest to complete 150 dismissals in Test cricket in 35 matches when he took a catch to dismiss Karunaratne in the morning session. Australia's Adam Gilchrist had reached the milestone in 36 Tests.
Sri Lanka reached 338 in its first innings and bowled South Africa out for 124, securing a 214-run first innings lead.
Sri Lanka won the first Test in Galle by 278 runs.