Nationals closer Sean Doolittle was all smiles after getting the final outs of Washington’s 7-4 win over the Mets. (Al Bello/Getty Images)

The bullpen door swung open, and out stepped Sean Doolittle. The Washington Nationals’ closer jogged back to the mound he had trudged off two days earlier, after he blew a save and before he spoke of demons. New York Mets fans at Citi Field rose in derisive applause.

“It made me laugh. It made me smile,” Doolittle said. “In a weird way, it relaxed me a little bit. I’m at my best when I’m able to have a little fun with it.”

Doolittle entered Sunday afternoon’s game with a 15.00 ERA and 1 for 4 in save opportunities against the Mets this season. He had a 1.90 ERA and a 24-for-26 save conversion rate against ­everyone else.

The first batter hit the ball hard but into a glove. The second didn’t hit the ball at all and returned to the dugout shaking his head. The third grounded to the shortstop, and when the ball nestled in the webbing of first baseman Matt Adams’s glove, it secured the Nationals’ 7-4, sweep-averting victory. It was the first time in their past six tries that the Nationals led the Mets in the eighth inning and emerged with a victory.

The Nationals’ six-game losing streak to the Mets was over. The intense series with late comebacks, playoff implications and an atmosphere to match was over. The 10-day, three-city road trip was over at 5-4.

“This is going to be a dogfight all year long,” Manager Dave Martinez said. “It’s fun, and I love it.”

Sunday’s fun didn’t come without a scare. Outfielder Juan Soto rolled his right ankle running the bases in the seventh inning and had to leave the game. X-rays were negative, and the Nationals will treat Soto as day-to-day. Soto was upbeat after the game and said he might feel good enough to play Monday night against ­Cincinnati at Nationals Park.

In Soto’s stead, Victor Robles hit a lead-padding, two-run homer in the ninth to help sweeten a series that had delivered two heartbreakers both stunning and predictable. First came Friday and Doolittle allowing four runs in the ninth to get walked off. Then came Saturday and Fernando Rodney allowing the tying home run and the eventual winning run to reach base in the eighth. Then came Sunday, offering the Nationals absolution.

It was simple. Win and ­reestablish a 1½ -game lead over the Mets in the National League wild-card race; lose and fall a half-game behind them. Despite everything, the Nationals left Citi Field having allowed the hottest team in baseball, which entered Sunday having won 15 of 16, to gain just a single game in the standings.

Doing so was no small feat. The Nationals needed their No. 3 available starter, Aníbal Sánchez, to beat Jacob deGrom, the reigning NL Cy Young Award winner, who is better against the Nationals than he is against pretty much everyone else. In 17 career starts against the Nationals entering Sunday, deGrom had a 2.44 ERA; in his two previous starts against Washington this season, he allowed one run in 12 innings.

But the Nationals jumped out to a lead Sunday by doing what Martinez had praised the Mets for all series: putting the ball in play. In the first, the Nationals had the bases loaded with two outs when newly acquired Asdrúbal Cabrera smoked a grounder toward first. Mets first baseman Pete Alonso laid out to stop the ball but threw it away as deGrom ran to cover the base. The Mets scrambled, the Nationals sprinted, the bases unloaded, and Cabrera ended up on third.

Almost as quickly as the ­Nationals gained the lead, though, they gave it back. With two outs and two on in the second inning, Sánchez allowed three straight hits — one of which was a bunt single by deGrom — to knot the score at 3.

The Nationals did not scratch deGrom again, but they did make him work. They fouled off tough pitches over and over, running up deGrom’s pitch count and putting runners on. The right-hander left after the fifth at 101 pitches — the first time he had been chased before completing seven innings since the all-star break.

The score remained deadlocked into the seventh, when the Nationals loaded the bases with one out. Adams struck out for the fourth time, but then Cabrera delivered again.

“I tried to hit the ball to the other side of the field,” he said. “I got jammed.”

Martinez praised the utility man’s ability to stay inside a fastball and hook a two-run liner down the right field line. But even when things went right for the Nationals, they went wrong, too. Soto was injured rounding third base and was tagged out. The young outfielder stood on the field for a few moments, shaking his legs, trying to loosen up. He grabbed his glove and began to jog to left field but stopped after a few steps. It was too much, and he exited the game.

The Mets got a run back in the bottom of the inning, but Daniel Hudson relieved Hunter Strickland with runners on first and second and two outs and stranded them. The Nationals’ lead remained intact.

“When you’re playing a team that’s that good and that hot, you’re going to have to absorb some punches,” Doolittle said. “Just because you take a lead doesn’t mean they’ll roll over. That’s what we did in the ­seventh.”

The eighth has given the ­Nationals so much trouble this season, but Wander Suero got the Mets in order. Doolittle polished off the ninth in what he called “an important outing for me.”

In the first two games of the series, the bullpen was the culprit. In the last one, it became one of the main reasons for the win. The intensity of these games at the end of a long road trip left the Nationals drained.

“Let’s get on that train,” Martinez said, “and go home.”