Nationals players mob Jayson Werth after his game-winning hit. (Toni L. Sandys/The Washington Post)

As soon as the ball flew off Jayson Werth’s bat with two outs in the ninth inning Sunday at Nationals Park, Anthony Rendon turned and took off from first base.

The ball rolled into the left field corner, and Rendon wasn’t stopping after he rounded second. The right arm of third base coach Bobby Henley spun like a windmill. And once Rendon ran through home plate to secure the Nationals’ 5-4 walk-off win over the Milwaukee Brewers, Werth pumped his fist around second base and ran toward his teammates.

They knocked off his helmet, like they did with Rendon, and tugged at his jersey. Hugs and high-fives abounded. Werth wouldn’t have had it any other way.

“That’s what it’s all about, right?” he said afterward, sitting at his locker. “That’s why we do this, I think. If you find yourself in that situation and you don’t want to be there, I think you’re in the wrong line of work.”

The Nationals claimed the series over the Brewers, one of the best teams in the National League, after twice recovering from some pitching woes. Starter Gio Gonzalez struggled early, knocked out after notching only 10 outs, and Rafael Soriano blew his third save of the season in the ninth inning. But thanks to a three-run fourth inning powered by Ryan Zimmerman’s game-tying home run and Werth’s game-winning double, the Nationals walked off the field in exhilaration.

Nationals left fielder Bryce Harper catches a hit by the Brewers’ Lyle Overby in the seventh inning. (Toni L. Sandys/The Washington Post)

Washington’s 11th victory this season in its final at-bat also kept it in a virtual tie with the Atlanta Braves atop the NL East. The Nationals moved to a season-high 10 games over .500 and have the NL’s best winning percentage.

“Once the all-star break is over, it’s time to really prove that we’re a good team,” reliever Craig Stammen said. “You take that one day at a time and play each game as best you can, and in the end you hope you’re in the lead. I think we’re well on our way to doing that.”

The Nationals rallied from an early deficit created by Gonzalez.

Rendon’s stellar sliding stop-and-throw from second saved a run in the first inning, but Gonzalez’s performance was a sign of what was to come. After 10 days in between starts, Gonzalez struggled to find a consistent arm slot, and his command wavered constantly.

The left-hander reached a career milestone with his strikeout of Rickie Weeks to start the third inning — the 1,000th of his career. He became the first pitcher to accomplish that feat in a Nationals uniform, and the ball was tossed into the dugout for a keepsake. But after that fifth strikeout of the game, Gonzalez and the Nationals’ lack of awareness on defense proved costly.

Gonzalez issued back-to-back walks to Ryan Braun and Aramis Ramirez. With two men on, Braun was running on the pitch when Jonathan Lucroy hit a soft grounder toward the mound. Gonzalez fielded the ball and tossed it to first base for an out, but Braun raced home and scored on a ball that was hit no farther than 45 feet. A single by Khris Davis gave the Brewers a 2-1 lead.

“It’s a little tough one,” Gonzalez said about the long layoff from the all-star break. “I’m not going to use that as an excuse. But I still have to find a rhythm. I still have to throw strikes. I still got to attack the strike zone. Falling behind on every hitter, it’s obvious it’s gonna show.”

Gonzalez lasted only one out into the next inning before Manager Matt Williams decided he had seen enough. After giving up a leadoff double to Jean Segura and a sacrifice bunt by starting pitcher Yovani Gallardo, Williams came out of the dugout to get the ball from Gonzalez. The left-hander trudged off the mound after only 88 pitches.

Williams called on Stammen to eat innings, and the reliever gave the Nationals a chance to get back in the game but not before another Nationals defensive lapse. With the infield in and one out, Carlos Gomez hit a groundball to the left side of the infield. Ian Desmond quickly peeked at Segura, who stayed put, and fired across the diamond. But Segura then broke home, and Adam LaRoche made a low throw, giving the Brewers a 3-1 lead.

But the Nationals erased the deficit in the fourth when Zimmerman crushed a curveball into the first row of seats in right-center field. Zimmerman’s power has picked up slowly since his return from a broken thumb, and he is hitting .392 (20 for 51) this month. The Nationals took a 4-3 lead when Jose Lobaton alertly scored on a wild pitch from Gallardo.

“We picked each other up,” Zimmerman said.

After Drew Storen and Tyler Clippard fired scoreless innings, the game fell to Soriano in the ninth. With one out, he gave up a single to Scooter Gennett and then walked Gomez on four pitches. Pitching for the first time in eight days, Soriano left a 2-2 fastball up to Weeks, who smacked it into center field for a game-tying single.

“It was bad,” Soriano said. “I didn’t throw it where I wanted. The cutter wasn’t good [Sunday].”

After Soriano escaped the inning without further damage, the Nationals sent pinch-hitter Nate McLouth, Denard Span and Rendon to the plate. Span singled, but Rendon erased him when reaching on a fielder’s choice. Werth came to the plate and quickly jumped ahead of Brewers right-hander Rob Wooten. Werth let a 3-0 slider go by, then drilled a 3-1 fastball over the plate to left field.

Rendon, a smart and quick base runner, motored to second base. Once Rendon rounded second, Henley had to decide what to do.

“When I hit it, I knew Rendon was running, so I figured old ‘No Stop Sign’ Henley over there at third base, he’s just always waving guys around,” Werth said.

Coach Mark Weidemaier, who handles the team’s advance scouting, had told players they could take an extra base on Davis in left field.

“Two outs and we’re winning,” Henley said. “If he’s thrown out, we keep playing. With the at-bat we had and with our guys running the bases aggressively, just sticking with how we play.”

Once Rendon scored, teammates jumped over the dugout railing and mobbed Werth in the infield. His long hair covered his face after so much head patting. As they walked back toward the dugout, Soriano smacked Werth in the stomach, and Werth pulled him in for a hug. The team could rejoice in a dramatic victory and their ascent toward the top of the NL.

“It’s a good sign for the team,” Soriano said. “That happened a lot in 2012 [when I wasn’t here]. It’s a good sign.”