NEW YORK — You gotta hand it to Freddie Freeman, Shohei Ohtani and the Dodgers.

And not just because the Yankees certainly did.

When the Yankees let the Dodgers back into World Series Game 5, the Dodgers did what they’ve done all year — kept on going.

After taking advantage of three miscues to erase a five-run, fifth-inning deficit during one of the most memorable midgame meltdowns in baseball history, the Dodgers used eighth-inning sacrifice flies from Gavin Lux and Mookie Betts to beat the Yankees 7-6 on Wednesday night.

“In spring training this is what we said we were going to do and we did it,” Betts proclaimed, champagne stinging his eyes.

Aaron Judge and Jazz Chisholm Jr. hit back-to-back home runs in the first inning for the Yankees.

Alex Verdugo’s RBI single chased Jack Flaherty in the second, and Giancarlo Stanton’s third-inning homer against Ryan Brasier built a 5-0 Yankees lead.

In the dugout, the Dodgers remained focused.

“We were like just get one, chip away, chip away,” Freeman said.

Errors by Judge in center and Anthony Volpe at shortstop, combined with pitcher Gerrit Cole failing to cover first on Betts’ grounder, helped the Dodgers score five unearned runs in the fifth.Of the 234 teams to trail by five or more runs in a Series game, the Dodgers became just the seventh to win.

“This is going to sting forever,” Yankees manager Aaron Boone said. “I’m heartbroken.”

After Stanton’s sixth-inning sacrifice fly put the Yankees back ahead 6-5, the Dodgers loaded the bases against Tommy Kahnle in the eighth before the sacrifice flies off Luke Weaver.

Judge doubled off winner Blake Treinen with one out in the bottom half and Chisholm walked. Manager Dave Roberts walked to the mound with Treinen at 37 pitches.

“I looked in his eyes. I said how you feeling? How much more you got?” Roberts recalled. “He said: ‘I want it.’ I trust him.”

Treinen retired Stanton on a flyout and struck out Anthony Rizzo.

Walker Buehler, making his first relief appearance since his rookie season in 2018, pitched a perfect ninth for his first major league save.

When Buehler struck out Verdugo to end the game, the Dodgers poured onto the field to celebrate between the mound and first base, capping a season in which they led the big leagues with 98 wins.

With several thousand Dodgers fans remaining in a mostly empty stadium, baseball Commissioner Rob Manfred presented the trophy on a platform quickly erected over second base.

Ohtani, the Dodgers’ record-setting $700 million signing and baseball’s first 50-homer, 50-steal player, went 2 for 19 with no RBIs and had one single after separating his left shoulder during a stolen base attempt in Game 2. Ohtani went through the clubhouse pouring champagne on teammates and having it sprayed on him

“We were able to get through the regular season, I think, because of the strength of this team, this organization,” he said through a translator. “The success of the postseason is very similar.”

Freeman hit a two-run single to tie the Series record of 12 RBIs, set by Bobby Richardson over seven games in 1960, and was voted Series MVP. With the Dodgers one out from losing Friday’s opener, Freeman hit a game-ending grand slam reminiscent of Kirk Gibson’s homer off Hall of Fame A’s closer Dennis Eckersley in 1988’s Game 1 that sparked the Dodgers to the title.

The Dodgers earned their eighth championship and seventh since leaving Brooklyn for Los Angeles — their first in a non-shortened season since 1988. They won a neutral-site World Series against the Rays in 2020 after a 60-game regular season and couldn’t have a parade because of the coronavirus pandemic.

These Dodgers of Ohtani, Freeman and Betts joined the 1955 Duke Snider and Roy Campanella Boys of Summer, the Sandy Koufax and Don Drysdale era that spanned the three titles from 1959-65, the Tommy Lasorda-led groups 1981 and ’88, and the Betts and Clayton Kershaw champions of 2020.

Ending a season that started with a gambling scandal involving Ohtani’s interpreter during the opening series in South Korea, Roberts won his second championship in nine years as Dodgers manager, matching Lasorda and trailing the four of Walter Alston. The Dodgers won for the fourth time in 12 Series meetings with the Yankees.