The Jets got their quarterback back Sunday and looked like a legitimate NFL franchise again.

Maybe Sam Darnold is a magic wand after all.

Darnold returned to the starting lineup, and the Jets got their groove back, notching a 24-22 upset victory over the Cowboys at MetLife Stadium to give the Jets their first win of the year and snap a seven-game losing skid that dated back to last season.

Darnold threw two touchdown passes in the first half, including a 92-yarder to Robby Anderson, and the Jets jumped out to a 21-3 lead. It looked like the Jets might coast to victory, but the Cowboys — who have lost three straight after winning their first three — managed a furious second-half comeback to set up a dramatic finish.

Cowboys quarterback Dak Prescott cut the Jets’ lead to two points when he scored a 4-yard run with 43 seconds left. On the two-point conversion, defensive coordinator Gregg Williams called for a blitz and safety Jamal Adams came flying up the middle and forced Prescott to rush his throw to tight end Jason Witten. The pass fell to the MetLife Stadium turf and set off a celebration.

“We had to make the play,” coach Adam Gase said. “It wasn’t going to be given to us.”

The Jets finally finished a game after blowing so many fourth-quarter leads in recent years, including Week 1 of this season.

The win gave Gase his first as the Jets coach and puts them at 1-4. It is hardly time to start talking about a playoff run, but just looking like an NFL team again after recent weeks is enough of a reason to celebrate.

The Jets managed 382 total yards and 20 first downs. In their previous two games combined, they had 233 total yards and 15 first downs.

The difference was easy to spot. Darnold — whom offensive coordinator Dowell Loggains said earlier this week was not a magic wand — showed no signs of rust after missing three games as he dealt with mononucleosis. He completed 22 of 32 passes for 338 yards, two touchdowns and one interception.

“We had a quarterback that knew what he was doing,” left tackle Kelvin Beachum said.

Darnold connected with Demaryius Thomas for 17 yards on the first play from scrimmage. It was a signal to everyone that the offense could function again.

“I definitely felt like myself out there, especially after the first play to DT, that pass I made to DT, I knew we were going to have a good game offensively,” Darnold said.

Darnold made everyone around him better. Gase looked like a better coach when Darnold and not Luke Falk was executing his plays. The offensive line, which gave up 10 sacks last week, allowed only two this week. The receivers ran the right routes. The price of beer at MetLife was lower … OK, maybe he did not fix everything.

As good as Darnold was in the first half, the Jets defense played a huge role in securing this victory. Dallas entered with the No. 1 offense in the NFL. The Jets kept the Cowboys out of the end zone until the fourth quarter. Prescott finished 28 of 40 for 277 yards and the one rushing touchdown. Stud running back Ezekiel Elliott ran for 105 yards and one touchdown on 28 carries.

“It’s just one [win] obviously, but it’s a good one,” Adams said. “Obviously, it’s [against the team from] my hometown. I am very excited about it, How ‘bout them Cowboys? I am excited. It’s just another stepping stone. Everybody is counting us out, everybody didn’t have faith in us, including some of ours as far as fans and we just want to get that faith back into us.”

The Jets built an early lead with a 14-play drive finished off by Le’Veon Bell’s 2-yard touchdown run, his first rushing touchdown as a Jet.

A monster sequence for the Jets came late in the second quarter. Rookie Quinnen Williams and Adams combined to stop Prescott on a fourth-and-2 run at the Jets’ 7. The offense took over at their own 8 and Gase dialed up a long ball. Darnold hit Robby Anderson for a 92-yard touchdown, the second-longest play in team history.

“I think it was huge,” Anderson said. “I think it was a big spark. Hopefully it’s a turning point in our season. I think that play gave everyone a lot of energy. I think we gained a lot of momentum off of it.”

The lead was 21-6 at halftime after another Darnold touchdown throw, this one a 5-yard pass to tight end Ryan Griffin.

The Jets held the Cowboys to field goals until the 6:30 mark in the fourth quarter. Elliott scored from 5 yards out to make it 21-16.

The offense answered with a seven-play drive that featured a 30-yard pass from Darnold to Jamison Crowder and an 18-yarder to Anderson. The drive stalled at the Cowboys 20, but Sam Ficken booted a 38-yard field goal to push the lead to 24-16.

Dallas mounted a 10-play drive that included three pass interference penalties on the Jets to help the Cowboys into the red zone. Prescott found the end zone for the touchdown, but the two-point try failed.

The Jets relished in pulling off the upset.

“Nobody counted on us to win that game,” linebacker Jordan Jenkins said. “Everybody bet on the Cowboys. They thought they were going to come here and we were going to get blown out and all this other stuff. Stuff didn’t go our way in the past but if we keep working we can shut everybody up.”