Steven Matz of the Mets walks to the dugout against...

Steven Matz of the Mets walks to the dugout against the Yankees at Citi Field on  Aug. 17, 2017. Credit: Jim McIsaac

After pitching through significant pain at times, according to sources, Mets lefthander Steven Matz will undergo season-ending surgery to reposition the irritated ulnar nerve in his left elbow, the Mets announced Monday.

Not until an exam by team physician David Altchek on Monday was it revealed that a problem with Matz’s ulnar nerve had caused the persistent pain. It’s the same injury that forced Jacob deGrom to undergo surgery last year. DeGrom has bounced back to turn in a stellar season, an encouraging sign for Matz.

Altchek recommended surgery, which will be scheduled in the next several days, ending what has been a painful season for the 26-year-old lefthander.

“It’s definitely nice to have an answer,” Matz said Monday night after the Mets’ 3-2, 10-inning loss to Arizona at Citi Field.

Earlier, Terry Collins said Matz likely would be scratched from his scheduled start Tuesday night and that he could be headed to the disabled list. But the manager offered vague and contradictory answers about the nature of the problem.

Collins conceded that Matz’s arm had been bothering him and had impacted his performance, but he stopped short of saying he was injured. “This is not an injury, this is not an injury,” Collins said. “It’s just an issue of he needs to shut it down for a little while . . . If he had to, he could pitch tomorrow. We’re going to probably put him on the DL.”

Later, Collins said he was “surprised” by the diagnosis.

General manager Sandy Alderson did not make himself available for comment after another episode that has raised questions about how the Mets handle injuries, especially to a pitching staff that has wilted beneath the weight of health issues.

Matz said he is optimistic about bouncing back from surgery, especially after speaking with close friend deGrom, who described “a really easy recovery, a really easy rehab.”

The nerve problem might explain an uncharacteristically poor season for Matz (2-7), who hasn’t won since June 28 and has seen his ERA spike to 6.08 — the highest of his career — in 66 2⁄3 innings.

Still, Matz downplayed the symptoms, chalking them up to pitching through discomfort after surgery to remove a bone spur last October. “We were feeling good enough to go out there every fifth day,” he said. “It wasn’t like I couldn’t throw. I was still able to throw, so it’s kind of a tough call.”

Previous MRI exams showed no structural damage to his left elbow, and this latest round of tests confirmed those results. They revealed no damage to his ligament or tendon, only a nerve condition that can’t be detected through MRI exams.

Matz insisted on powering through, perhaps in defiance of a reputation he’s gained for often being injured. And the Mets proceeded as if he were dealing with inflammation.

According to a source, Matz consented to multiple pain-killing injections, occasionally on game days, to quell the pain that came along with an elbow that at times swelled to the size of a grapefruit. The pain came and went, though the elbow bothered him mostly between starts.

Publicly, Matz refused to acknowledge any health issue despite clear signs of a problem.

Sources said Matz has been abbreviating bullpen sessions between starts at points this season — or skipping them altogether at times — to avoid placing extra stress on the elbow. At the time, Matz insisted that nothing had physically inhibited him from throwing as usual and that he was simply experimenting with his routine in an effort to turn around a horrific season.

Matz indicated that not throwing those bullpen sessions made it more difficult to put in the work to improve results. “I really wasn’t able to work on stuff that I wanted to,” he said Monday night.

Still, he continued to pitch despite performances that signaled the potential of an underlying problem.

One source described a belief by some in the organization that Matz was simply learning to get over the “mental hurdle” of pitching through pain. Indeed, injury has been a constant theme throughout his career.

Not long after he was drafted in the second round in 2009, Matz had Tommy John surgery. He then endured a rocky rehab that delayed his development. Another recent example came in spring training, when the Mets sent him to the DL with elbow inflammation, although he said he was battling a strained flexor tendon.

Nevertheless, Matz returned in June with some initial success. He was 2-1 with a 2.12 ERA in his first five starts but fell to 0-6 with a 10.19 ERA in his last eight.

On Monday, Collins repeated his effort to keep Matz from throwing his slider, a pitch the manager said has caused discomfort. Through his first three starts, Matz stayed away from the pitch, though he eventually resumed using it.

Collins said lefthander Tommy Milone, fresh off the disabled list with a knee injury, likely will start Tuesday night.

Matz, meanwhile, said he wouldn’t have changed anything.

“I don’t want to speculate and use it as an excuse because I was doing everything I could to go out there and pitch,” he said. “To say that this was affecting me on the mound, I don’t want to speculate because I really don’t know.”