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. Then they fought hard to pull it out in overtime.

Porzingis made a rookie error with 2.4 seconds left in the fourth quarter that ultimately forced the game to go to overtime. But in the extra period, the Knicks picked up their teammate and held off the Jazz, 118-111, Wednesday night at the Garden.

Carmelo Anthony played brilliantly to lead the Knicks, who were down 11 late in the third quarter. He had 30 points, nine assists and seven rebounds, and he got plenty of help. Robin Lopez scored 22 points and grabbed 12 rebounds. Derrick Williams scored 18 off the bench, including seven in overtime.

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

He suffered a sore right foot in the fourth quarter of Monday’s double-overtime win over Philadelphia. This time, Porzingis fouled out late in the fourth when he committed a foul on a three-point try.

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

Porzingis had 16 points in 29 minutes. His foot looked fine when he threw down an offensive rebound with two hands.

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

In overtime, Anthony fed Williams for a three-pointer and 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 lead on a Lopez putback.

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 one foul shot to make it 116-111 with 15.9 seconds left.

In regulation, the Knicks blew a five-point lead in the waning seconds. Anthony found Langston Galloway for a three-pointer that put them up 95-90 with 36.2 seconds to go, but they squandered it.

After a Trey Burke three-pointer 10 seconds later, Anthony drove past Rudy Gobert — who stuffed him earlier — and slammed it with 15.7 seconds to go to make it 97-93. Rodney Hood then drilled a step-back three with 10.6 seconds remaining to cut it to 97-96. Porzingis put them up three with a pair of free throws but gave it right back with the foul on Hayward.

The Knicks were down 11 late in the third, but Lopez, playing with four subs, helped lead them back. That group cut it to 80-76. Anthony and Porzingis re-entered with 7:10 remaining and were instrumental in the Knicks’ taking their first lead since the first quarter. Porzingis made two foul shots, Anthony fed Williams for a slam and then Anthony drove and dunked, giving the Knicks an 82-81 lead with 5:11 left.

The teams exchanged baskets and leads on the next four trips. Hayward hit a jumper. Anthony scored inside. Trey Lyles buried a corner three-pointer. Porzingis knocked down a three from the top of the key that put the Knicks up 87-86 with 3:54 left. Gobert made two free throws to give Utah a one-point lead. Galloway answered with a driving layup to give the Knicks an 89-88 edge with 3:02 to go.

It stayed like that until Anthony threw a cross-court pass to Arron Afflalo for a three-pointer that made it 92-88 with 1:01 left.