The Washington Nationals didn’t play good baseball Friday night, and in the end they didn’t need to.

They were lucky the Miami Marlins were in the opposite dugout. They were lucky to win, 7-6, despite doing just about everything they could to lose. Daniel Hudson blew a ninth-inning save by giving up a two-run homer to Starlin Castro. The bullpen had already blown a two-run lead in the seventh when Hunter Strickland gave up one run and left the bases loaded behind him and Roenis Elías soon walked in another. The defense made two errors and could have been charged with a third. The offense flatlined in the middle innings, one batter after another, but the Nationals would come out on top anyway.

A two-run, walkoff single from Anthony Rendon did the trick. His teammates, having stumbled all night, mobbed him around first base, and Victor Robles soon showered him with a cooler’s worth of Gatorade. The fans who stuck around chanted “M-V-P!” as he did an on-field interview.

Manager Dave Martinez also reached for those three letters — MVP — when he discussed Rendon after the game. So did Hudson, thankful that Rendon got him off the hook. The Nationals third baseman, now hitting a National League-best .333, is two back of the league lead in RBI with 107. His most recent three helped the Nationals sidestep themselves and flush one of their worst performances since they revived this season in late May.

“A little sloppy,” Martinez said of his team’s overall performance. “But you know what? We never quit. They kept playing and kept playing.”

Hosting the Marlins, a historically bad club, could have appeared like a break on the Nationals’ schedule, a final tuneup before the pennant chase really begins. The Marlins have two eyes on the future. This was their 13th straight road loss, a franchise record. But there are no unimportant games for Washington this time of year. Every wild-card contender is playing a big series this weekend. The New York Mets and Philadelphia Phillies are squaring off, the Milwaukee Brewers are visiting the Chicago Cubs, and the St. Louis Cardinals have a favorable matchup with the Cincinnati Reds.

And because the Mets, Phillies, Brewers, Cubs and Cardinals could wind up jousting with the Nationals for a playoff spot or home-field advantage in the wild-card game, Washington has to keep pace. A lesser opponent isn’t an excuse to relax; it’s an opportunity not to be missed.

But the Nationals didn’t bring a ton of urgency into this series opener. Aníbal Sánchez’s 10th pitch was a misplaced cutter, getting way too much of the plate, and Harold Ramirez lifted it over the left-center fence for a solo homer. Sánchez ultimately gave up two runs in five strenuous innings. The offense couldn’t take full advantage when Elieser Hernández, the Marlins’ 24-year-old starter, had shaky command in the early innings. And the defense, as a whole, was as bad as it has been in a long time.

When the Nationals were struggling through spring, on their way to a 19-31 start, errors were almost expected. But once the Nationals were back to full strength and the season turned around, an improved defense could have been taken for granted. Then Trea Turner fielded a routine groundball in the third, looked up and sent an underhand flip over Asdrúbal Cabrera’s head. In the same inning, Matt Adams bobbled a grounder and Sánchez, having run from the mound to cover first base, bobbled Adams’s toss onto the dirt. Then Gerardo Parra spun around once, then twice, and misplayed a flyball that bounced into the Nationals’ bullpen for an automatic double.

The Marlins, however, couldn’t use the extra outs to pull away. But just because the Nationals weren’t burned by their early mistakes and won doesn’t mean they didn’t happen at all.

“That’s over, done with,” Martinez said. “Let’s come back tomorrow and go 1-0.”

Martinez watched a two-run lead disappear in the seventh. He watched his team regain it in the bottom half on Juan Soto’s second RBI double, but even that wasn’t safe. Hudson gave up an infield single to start the ninth inning. He then gave up a two-run shot to Castro but did settle in to keep the Nationals within a run.

It was a gap the offense could close. Howie Kendrick opened the inning by ripping a single off Marlins reliever Ryne Stanek. Turner followed by working a full-count walk. After Parra sent a bunt straight into the air and was retired as a result, a passed ball moved runners to second and third. Rendon stepped in, the stadium loud around him, and pulled a low-and-away slider into left.

The Nationals’ total effort Friday — creaky, careless at times, almost fateful — was an aberration after how well they have played for three months. The miscues came from all directions. Then they were erased with one final swing, and Rendon could have spoken for his team when relaying, rather eloquently, what went through his mind once Turner slid in headfirst for the victory.

“We won,” Rendon said before he was asked whether that was it. “Yeah, and we get to go home.”