Mathews scored a defiant 89 off 182 balls as Sri Lanka closed the third day on 283 for nine, trailing South Africa by 172 runs.
Mathews’ innings, which included 14 fours and a six, finally ended when he went for a slog sweep off Imran Tahir and was bowled.
The skipper and Rangana Herath shared an eighth- wicket partnership of 71 off 117 balls that saved Sri Lanka after Steyn had reduced them to 201-7 replying to South Africa’s first innings of 455-9 declared.
Herath was unbeaten on 12 at stumps having lost Suranga Lakmal for six off the last ball of the day to Morne Morkel to Quinton de Kock behind the wicket.
“Bowling on the sub-continent is so hard,” said Steyn after the close. “But I kind of enjoy that challenge, and this is one of the few places in the world where I haven’t taken five-fors. “I’m lucky – I am short, skiddy, so my bouncer doesn’t get up. And if there is a little something on the ball, whether it’s reversing or just swinging, I always have a chance.”
Sri Lanka were rocked in the final session by Steyn who picked up three wickets for eight runs off five overs, three of them maidens, to put South Africa firmly in control.
Steyn sent back Lahiru Thirimanne for 38 flashing at a delivery outside off stump to present De Kock with an easy catch and ending a threatening partnership of 54 with Mathews.
Steyn then dismissed Dinesh Chandimal for six and Dilruwan Perera for a duck off successive overs to claim his 23rd five-for in an innings as Sri Lanka slid from 190-4 to 201-7.
Chandimal pulled a short ball straight to Alviro Petersen at mid wicket and Perera was caught off the inside edge by De Kock.
Mathews edged Sri Lanka closer to getting past the follow-on mark by hitting Morne Morkel for five successive fours in an over that cost 20 runs.
Sri Lanka fought back after losing the wicket of Upul Tharanga for 83 in the afternoon session to be 182 for four wickets at tea.
The wicket of left-handed opener Tharanga, making a return to test cricket after nearly seven years out of the side, was South Africa’s only success in the middle session.
Tharanga was beaten by a well-flighted delivery from off-spinner JP Duminy as he advanced down the wicket and was stumped by De Kock after hitting 14 fours and a six in his 155-ball knock.
Thirimanne and Mathews deprived the South African bowlers from making any further breakthroughs, putting up staunch resistance against a varied attack of pace and spin.
A brief rain interruption in the morning was followed by the fall of the home side’s two most experienced batsmen, Kumar Sangakkara for 24 and Mahela Jayawardene for three.
Sangakkara and Tharanga had added 59 for the second wicket before the former dragged a short ball from Morne Morkel onto his stumps while attempting a pull shot just two balls after play had resumed.
Paceman Steyn added to South Africa’s joy when he picked up his second wicket of the morning, trapping Jayawardene lbw off the last ball before the lunch break.
Steyn had earlier accounted for opener Kaushal Silva (eight), who was tempted into a hook shot and caught by a diving Vernon Philander at fine leg
(Editing by Tony Goodson)