DETROIT — A flung octopus hit the ice during the national anthem on Sunday, continuing a tradition at Joe Louis Arena, which is closing at the end of the season.

Then Rangers captain Ryan McDonagh scored twice — once shorthanded — and Derek Stepan snapped a 23-game goal-less streak as the Blueshirts made their last visit ever to the old barn even more memorable.

They left The Joe with a 4-1 win.

McDonagh, who hadn’t scored a goal in 12 games, made it 1-0 with 27 seconds left in the first period with Steven Kampfer in the box for tripping Andreas Athanasiou. Kevin Hayes took the puck to the net and his backhander at the right post deflected off McDonagh’s stick, which was in the right spot in the crease.

It was the team’s ninth shorthanded goal of the season, which is the second-most in the league, behind the Nashville Predators (10). The Blueshirts then scored twice in 37 seconds late in the second period to put the Red Wings behind the eight-ball and Stepan sent a soft shot past Petr Mrazek on a power-play with 3:59 left in regulation on a cross-ice pass from Chris Kreider.

“Flubbed it,” said a relieved Stepan. “It was a rough stretch, I’ll tell you that; I had 10 shots the other night and tonight I didn’t think I was that good. Like I said, in conversations with people I kinda knew it wasn’t going to such be a pretty goal, kind of a float-in.”

advertisement | advertise on newsday

Subbing for Henrik Lundqvist, who is sidelined for two to three weeks with a hip strain, Antti Raanta made 23 saves and the Rangers rebounded from a 4-3 defeat in Carolina and upped their record to 19-5-1 after a loss.

“One game at a time, one shot at a time,” said Raanta (14-6). “Just trying to do the same thing as I’ve been doing all year, although you’re going to be playing more.”

Trailing after the first period, the Red Wings had tied it on a power play with Marc Staal in the box for elbowing at 4:52. The goal came quickly, when Henrik Zetterberg started a tic-tac-toe play from the left boards, first to Gustav Nyqvist, who touched it to Justin Abdelkader and on to former Islander Frans Nielsen.

McDonagh’s second goal came when he persisted in attempting a pass in from behind the net and then jammed in Staal’s rebound. A lefty who has been playing the right side recently, McDonagh said he was “a little more comfortable offensively, and playing with an experienced guy like Marc, he’s very talkative and we’ve been working through some plays.”

And 37 seconds later, Hayes fired J.T. Miller’s pass in for a two-goal cushion at 17:27 of the second period. The Blueshirts primarily spent the third holding onto the lead. They improved to an NHL-best 25-9-0 on the road.

“We played the percentages, made the plays when we had the puck, and Mac had a real strong game, Step finally scored one and thought Kevin Hayes and that line (with Miller and Michael Grabner) was good,” said Vigneault.

Grabner, who returned from missing five games with a hip/oblique injury, had two assists.