The Knicks survived a rare mistake by Kristaps Porzingis that could have cost them a game they led by five points in the waning seconds of regulation. Porzingis watched from the bench in overtime, prayed for his teammates to pick him up, and his prayers were answered.

Porzingis made a rookie error with 2.4 seconds left in the fourth quarter that ultimately forced the game to go to an extra session. But in the overtime, Carmelo Anthony and the other Knicks fought and scrapped and held off the Jazz, 118-111, Wednesday night at the Garden.

The Knicks were down 13 in the first half and 11 late in the third quarter, but played with a refuse-to-lose mentality when it mattered. Anthony was brilliant, leading the Knicks (22-22) in all areas. He had 30 points, nine assists and seven rebounds.

“It was a major win for us,” Anthony said, “especially how the game was going the first couple of quarters. For us to stay composed and bounce back the way that we did, chip at that lead offensively and defensively buckling down, it was a big win for us.”

Anthony, who passed Larry Bird for 31st on the NBA all-time scoring list with 21,793 points, got plenty of help. Robin Lopez had 22 points and grabbed 12 rebounds. Derrick Williams scored 18 off the bench, including seven in overtime.

$relatedItem.embed_code.value

It was the second straight game the Knicks needed overtime to win and the second time Porzingis was a spectator when the game was decided.

advertisement | advertise on newsday

He suffered a sore right foot in the fourth quarter of Monday’s double-overtime win over Philadelphia. Last night, Porzingis fouled out with 2.4 seconds in regulation with 16 points after he committed what could have been a costly personal.

The Knicks were up 99-96 after Porzingis’ two free throws. They needed a stop, but Gordon Hayward faked Porzingis into the air and he bit and jumped. Hayward rose up, Porzingis hit him, and Hayward made the three free throws to tie it. Anthony’s running hook shot at the buzzer hit the side of the backboard.

“My instincts are to go up and try to block it,” said Porzingis, who said his foot felt fine most of the game. “I got to stay on my feet and use my length and try to bother his shot. I let my instincts play and I just jumped up there. I got to learn from those.

“Once that play is over, I’m on the bench, I’m thinking how I can help my team, cheer for my team and pray that we win the game.”

Rodney Hood led Utah (18-24) with 29 points. Hayward added 27.

Knicks videos

In overtime, Anthony fed Williams for a three-pointer that gave the Knicks a 106-103 lead with 2:25 remaining. They went up 108-103 on the next trip on a Lopez hook shot. After Hood scored to make it a three-point game, Williams fought inside for an offensive rebound and secured it. He went up strong, scored and was fouled. His free throw gave the Knicks a 111-105 lead with 1:01 left. They took a 113-106 edge on a Lopez putback.

“I thought our guys just kept playing and finding ways to continue to make the necessary plays to win,” Derek Fisher said.

Utah closed to 115-111 and had the ball with 29.1 seconds to go. But the Jazz misfired, and the Knicks eventually got possession after a jump ball. Williams made a foul shot to give the Knicks a 116-111 lead with 15.9 seconds left.

The Knicks squandered a five-point lead in regulation after going ahead 95-90 on Langston Galloway’s three-pointer, off a pass by Anthony, with 36.2 seconds to go. Trey Burke drilled a three 10 seconds later,. Then Anthony drove past Rudy Gobert — who stuffed him earlier — and slammed it with 15.7 seconds left to make it 97-93.

Hood drilled a step-back three-pointer with 10.6 seconds remaining to cut it to 97-96. Porzingis put the Knicks up three with a pair of free throws but gave it right back with the foul on Hayward. Porzingis was lucky it didn’t cost the Knicks.

advertisement | advertise on newsday

“It’s a learning experience for a young player,” Fisher said. “You can’t learn until you’re in these situations. I guarantee you the next time he’s in that position he’ll remember this time where he didn’t handle it the right way and be better the next time he’s in that spot.”