Carmelo Anthony limped a little as he jogged down the court late in the second overtime. After missing the previous two games with a sprained right ankle, the last thing Anthony needed was to log nearly 50 minutes.

But the Knicks needed him when what appeared to be a laugher turned into a thriller — and when they lost Kristaps Porzingis, who limped off in the fourth quarter with a foot injury.

The Knicks squandered an 18-point third-quarter lead and were in a fight to avoid an embarrassing defeat. It took two overtimes, but they held off the 76ers, 119-113, in a Martin Luther King Jr. Day matinee.

See alsoBoxscore: Knicks 119, 76ers 113 (2OT)

“There’s a lot of bad in the win,” Porzingis said. “It’s a good win. We’ll take the win. But there’s a lot of stuff that we’ve got to learn from that.”

Perhaps one good thing is that Porzingis said he didn’t suffer a serious injury after landing awkwardly in the fourth quarter. He said his right foot is bruised and hurts on the bottom and side, but he expects to return Wednesday night against the Jazz.

Anthony hopes he can play too, but he said his ankle was sore.

advertisement | advertise on newsday

“I was kind of using this as a rehab game to come back and play a little bit and gauge where I was at,” he said. “Tomorrow is a new day, so we’ll see.”

A little turned into 49 minutes.

Anthony didn’t have his usual lift. He was stuffed three times inside and missed 21 shots overall, including what would have been the game-winner in the waning seconds of the first overtime. But he made some huge plays. The biggest was a three-pointer with 3.4 seconds left in regulation to tie it at 96 and force overtime.

Knicks videos

“I live for moments like that,” Anthony said. “Some games it calls for me to take that shot and some games it don’t. It was one of the games where it calls for me to take it and make it.”

Anthony also took the ball away from Isaiah Canaan with 26.9 seconds left in the first overtime and connected on a pull-up jumper with 1:11 left in the second OT to give the Knicks a 117-111 lead.

He finished 7-for-28 from the field and 3-for-7 from the foul line and had 19 points, seven rebounds and seven assists.

“It hindered me most of the game,” he said of the ankle. “Just not being able to shoot with balance, push off, things that I’m normally capable of doing. I couldn’t do that.”

Arron Afflalo came up big, scoring 15 of his 25 points after the third quarter. Porzingis finished with 16 points and 12 rebounds. He left the game with 2:40 left and returned for one play — Anthony’s tying three-pointer. Porzingis said he could have played but that the trainers told him to sit.

advertisement | advertise on newsday

The Knicks (21-22) were fortunate to win the game and snap their two-game skid. The 76ers (5-38) missed 10 free throws in the fourth quarter and overtime sessions.

Hollis Thompson was fouled by Lance Thomas while taking a three-pointer late in regulation and missed all three free throws. Of course, he buried a three-pointer on the 76ers’ next possession. In the first overtime, Robert Covington banked in a tying three-pointer with 7.4 seconds left. It was that kind of game.

The Knicks couldn’t contain quick guards Canaan and Ish Smith. Each scored 16 points — Smith also had 16 assists — and both got in the paint whenever they wanted and made plays.

Langston Galloway had only eight points, but four came in the second OT, when he also had a big block on Canaan.

Canaan and Smith sparked the 76ers’ comeback from 18 down with a 32-12 run that spanned the third and fourth quarters, making Anthony play a season-high number of minutes on a bum ankle.

advertisement | advertise on newsday

“We went up 18, so that was kind of a moment where I could decompress a little bit,” Anthony said. “I looked up and the lead kept getting cut and cut and cut. At that point I knew it was going to be a longer night.”