PHILADELPHIA — When Tyson Foerster scored in the first minute of the second period Thursday night, everything was set up for a storybook ending for the Flyers.

Somebody must have forgotten to tell the Detroit Red Wings.

The visitors came back to retake the lead in the middle frame on the way to a 4-2 victory over the Flyers at Xfinity Mobile Arena.

The Flyers are just 6-25-5 when trailing after the second period.

Foerster, out for 49 games since early December, scored 31 seconds into the second period when he secured the deflection of an Owen Tippett shot and sent it past Detroit goaltender John Gibson.

The tie stayed in place until a setup pass deflected off Flyers defenseman Jamie Drysdale and right to Lucas Raymond on the left side of the net. Goaltender Sam Ersson, who had been 5-0-0 since Jan. 29, couldn’t get over in time to stop the shot with 1:31 left in the period.

In the period, Patrick Kane used a two-on-one to give the Wings a two-goal margin. Instead of passing, Kane kept the puck and beat Ersson with a short side shot at 9:37.

The Flyers weren’t done yet. Travis Konecny charged the net and was in place to have Porter Martone’s shot deflect off his body and into the net at 10:29 to cut the deficit to 3-2. Initially the goal was disallowed, but the Flyers challenged and won the plea.

Just when it looked the Flyers might do something special, Alex Debrincat scored his second goal of the night to decide it.

“We gave them much but what we gave them they put in the net,” coach Rick Tocchet said. “And we were missing the net way too much (20 missed shots). It’s been a problem this year. Loved the effort tonight but they had a couple big moments. And that’s really what it came down to, We had our chances, just couldn’t put them in.”

Martone’s assist was his first NHL point. He finished with nine shots on goal, giving him 14 in his first two pro games.

“He just gets the puck on net,” Tocchet said. “Gibson made some really good saves. He puts one on the net again (the Konecny goal) and we score. That’s something we really have to work on as a team. Got to hit the net. It’s really frustrating.”

Martone doesn’t sound intimidated by NHL competition.

“I liked the way our group kept going, no quit,” Martone said. “I think that’s something they’re preaching for the rest of the season. Just continue to keep going. I feel I could have scored a couple. I think I kind of found my game and kept on playing.”

Tocchet had good things to say about Foerster’s first game back after being sidelined four months following arm surgery.

“Look at the shot in the slot, that’s what he can do,” the coach said. “Like Martone, Foerster has that quick shot. It’s dangerous when he gets set up and hits the net.”

Foerster went in for post-game medical treatment and was not made available for comment.

Konecny liked that he saw of both Martone and Foerster.

“I played with him (Martone) at Worlds (last year) and you wouldn’t have known he was a junior kid,” Konecny said. “Playing for the first time with professionals. He kind of knows his role, he knows what he’s good at and sticks to it. He’s only going to get better.”

As for Foerster, nothing new there. He’s a sniper when he gets set in prime shooting areas.

“Too bad we couldn’t win for him,” Konecny said. “But I mean what a lift for the team. It’s frustrating we lost but it’s encouraging and you go to the rink tomorrow, there’s a euphoria around the locker room. Big lift, big boost. He’s a pure goalscorer. I’m not shocked.”

An unnecessary penalty by Sean Couturier led to Detroit scoring the first goal. Falling to the ice right along the end line in the Red Wing zone, Couturier reached out and committed a trip. Just 17 seconds later, Debrincat connected with a shot from just above the hash marks.

• • •

Short shots >> The Flyers head to New York for another crucial game against the Islanders Friday night. Dan Vladar is expected to start in goal. … Trevor Zegras hit the 60-point mark on Tuesday, making him just the fifth Flyer in 20 years to reach that milestone in his first year with the team. The others: Peter Forsberg, 75 points (2005-06); Daniel Briere, 72 (2007-08); Mike Knuble, 65 (2005-06) and Matvei Michkov, 63 (2024-45).Zegras picked up an assist on Foerster’s goal, extending his point streak to seven games, longest active in the NHL. … The Flyers’ scratches were Garnet Hathaway and Alex Bump.