The Capitals swarm defenseman Michal Kempny, third from left, after his go-ahead goal against the Red Wings on Sunday night. (Tim Fuller/USA Today Sports)

Defenseman Michal Kempny seemed to be skipping on the ice as he gleefully made his way to the Washington Capitals’ bench. His team has been struggling to score over the past week, so it was fitting that a shot all the way from the blue line ultimately ended its losing streak. And the goal came from a player who had never scored more than three goals in a season before this one, in which he already has five.

Washington snapped its three-game skid with a 3-2 win over the Detroit Red Wings on Sunday night. It wasn’t the Capitals’ most inspiring performance of the season — play was sloppy on both sides through the first two periods — but they delivered when it mattered, scoring twice in the third period to reclaim the lead.

“I liked our perseverance tonight, simplifying things when we needed to and getting pucks to the net and some scoring from some different people,” Coach Todd Reirden said. “It was important for us tonight.”

With the Red Wings up by a goal in the third period, center Travis Boyd carried the puck up the ice before relaying it to captain Alex Ovechkin, who drew two Red Wings to him in the left faceoff circle before dishing the puck back to Boyd on the right side. Boyd lifted both hands in the air, dropped his head back and screamed as he celebrated his fourth goal of the season 6:19 into the period.

Less than a minute later, the Capitals were awarded their third power play and an opportunity to take the lead, but the unit failed to convert and is now 1 for 31 in the past nine games. Kempny’s goal, with Ovechkin and right wing Tom Wilson screening Detroit goaltender Jimmy Howard, came 16:08 into the third period. ­Washington then staved off a ­Detroit power play with less than three minutes left to secure the win.

Although Kempny isn’t known for scoring a lot, he has been working on getting pucks through, something that paid off when his knuckling shot went off a Red Wings player’s stick and then wove its way cleanly to the back of the net. Having forwards posted in front of the net helped, too, something the Capitals emphasized with their five-on-five scoring down of late.

“We felt like our forwards were really fighting hard to get to the front of the net, and we didn’t deliver enough pucks there,” Reirden said. “That was the message in between the second and third, and we were able convert on that after.”

Reirden recently reunited the Capitals’ top line from last season’s playoffs, using center Evgeny Kuznetsov with Ovechkin and Wilson, and the trio made an early impact Sunday. With the puck pinballing in front of the net, Kuznetsov corralled it, and his spinning pass found Wilson on the right side for a one-timer 6:57 into the game. That was Wilson’s 12th goal of the season, leaving him just two shy of matching his career high set last season. More impressive is that he has played in just 22 games this year after he was suspended for the first 16 and then missed more time with an injury.

But the Red Wings responded to Wilson’s goal, pinning the Capitals in their own zone for several shifts. Detroit’s Anthony Mantha capitalized on a defensive-zone turnover by forward Jakub Vrana, and on a breakaway 14:08 into the game he beat goaltender Braden Holtby with a backhand through the legs to make it 1-1 at the first intermission.

Then on a rush in the second period, Red Wings defenseman Filip Hronek got behind Kempny, and Hronek tipped Dylan Larkin’s centering pass at the net front 6:48 into the period. Detroit took that 2-1 lead into the third.

“Obviously, the game wasn’t going that great — sloppy and all that,” Boyd said. “But I think we all said that if we just play the game the way we can for 20 minutes here, we can get this win.”

The Capitals were encouraged with their play after Friday night’s game in Dallas, an overtime loss. Washington held the Stars to 10 shots in the second and third periods, and Reirden was so pleased with the five-on-five play that he didn’t make any changes to Sunday’s lineup.

But as other teams have started to catch up to the Capitals in the Metropolitan Division, a fourth straight loss, no matter how well Washington may have played in it, would have been troubling. The team hasn’t lost four straight since March 2017.

Instead, the Capitals ended this three-game road trip on a high note — not with a perfect performance but with a resilient one.

“That’s what you’ve got to do in this league,” Wilson said. “It hasn’t been coming easy for us the last couple of games, and it’s huge to be able to turn that one around and finish on a strong note. It’s a tough league, and you’ve got to be sure you end those little skids. We pulled together, and guys did a good job chipping in there in the third.”