Mathews was at the crease on 107 with Niroshan Dickwella on 19 at the close in Galle as Sri Lanka, who lost the first Test by seven wickets, tried to make best use of a good batting pitch after winning the toss.
Anderson bowled with masterful control throughout and claimed the first three wickets, including opener Lahiru Thirimanne for 43 soon after lunch, that took his Test tally to 603 in 157 matches.
The 38-year-old Lancashire bowler finished the day with remarkable figures of 3-24 from 19 overs.
Mathews built crucial partnerships, including a 117-run stand for the fourth wicket with skipper Dinesh Chandimal who made 52.
Mathews, the former captain, acknowledged that England had put his side under pressure early in the day.
“I got into rhythm in the second innings of the first Test, so I thought if I get a good start, I can make it big,” said the 33-year-old all-rounder.
“Even though we are 229 for four, we can’t give it away tomorrow. If it is this hot tomorrow, it will start turning.
“We don’t know what a good score is — we will try to get as many as we can.”
England fast bowler Mark Wood trapped Chandimal lbw and the batsman went off after an unsuccessful review of the umpire’s decision.
Mathews reached his 11th Test hundred, and third against England, with a single off Jack Leach.
He put on 36 with Dickwella, holding out after England took the new ball in the 81st over.
“Mathews has been a thorn in our side a little bit. I am sure he has been using a wider bat than anyone else,” joked Wood.
“We have just got to keep hammering away and try to get an opening.
“It was hard work,” he said of the frustrating conditions and heat.
Anderson, who replaced fellow quick Stuart Broad was the only change for England, struck twice in one over in the morning session with the wickets of Kusal Perera and Oshada Fernando (0).
Perera was caught at first slip by England skipper Joe Root for six. Fernando only lasted four balls before playing a rising delivery onto his stumps for a duck as Sri Lanka slipped to 7-2.
Thirimanne and Mathews rebuilt the innings with a 69-run stand up to lunch, but Anderson broke through after the break when Thirimanne edged to Jos Buttler behing the stumps.
The series, which is being played behind closed doors, resumed last week after being cancelled in March 2020 due to the coronavirus pandemic.