When David Ortiz hit a home run in the eighth inning on Thursday, it felt like a consolation prize for the sellout crowd at Fenway Park. The Red Sox were still trailing the Yankees by three runs but, hey, at least the folks saw Big Papi hit one.
Ortiz, who is running out of his career, was not in the mood for any solace. He started shouting at his teammates when he got back to the dugout.
“Let’s go!’’ he said. “Let’s go!’’
Hanley Ramirez didn’t let Ortiz down. His three-run homer with two outs in the ninth gave the Red Sox a thrilling 7-5 victory against the Yankees.
“I was trying to go to the moon,’’ said Ramirez, who came close. He hammered a 99-m.p.h. fastball from Dellin Betances seven rows deep into the bleachers in center field.
The Sox now lead the Baltimore Orioles by two games in the American League East with 16 to play. They also threw a haymaker at the Yankees, who now trail by five games. New York blew a 5-1 lead.
“The rivalry hasn’t died,’’ Ortiz said.
Ramirez said it was his most exciting moment on the field since he was part of a Dominican Republic team that won the World Baseball Classic in 2013.
“Everybody was like, ‘We gotta win tonight. We gotta win tonight,’ ’’ he said. “It was one of the moments you have to enjoy. I let everything go.’’
It was the sixth career walkoff hit for Ramirez, who has 25 homers and 100 RBIs.
The Yankees wanted to avoid using Betances. Tommy Layne started the inning and struck out pinch hitter Aaron Hill. When Chris Young went up to pinch hit, Yankees manager Joe Girardi countered with righthander Blake Parker.
Young was hit by a breaking ball that glanced off his helmet. Girardi then turned to Betances.
Young took second unopposed. Dustin Pedroia than walked and the runners moved up again as the Yankees ignored them.
Xander Bogaerts tapped a ball back to the mound and the Yankees were able to get Young caught in a rundown for the second out.
Ortiz singled to center, driving in Pedroia and sending Bogaerts to third. Mookie Betts was next and he singled to left field to score Bogaerts.
With Ramirez up, a passed ball moved the runners to second and third. Then came his blast on a 3-and-1 pitch. The Red Sox celebrated wildly, drenching Ramirez in Gatorade.
“For me, that’s my best baseball moment,’’ Betts said. “Some other guys have been in the playoffs and won the World Series. I’m trying to get there. For me, that was at the top of the list.’’
Said Ramirez: “I’m not satisfied. We haven’t got the job done yet. We have to keep going. I’ll talk to you after November and ask me the same question.’’
Ortiz was 3 for 4 with a double and the home run that started the comeback. He now has 537 career home runs, putting him alone in 17th place all time. Ortiz had been tied with Yankees legend Mickey Mantle.
Ortiz has 34 home runs this season, tying Darrell Evans of the 1987 Detroit Tigers for the most by a player 40 or older.
The home run, a blast to center field in the eighth inning off Adam Warren, roused the Sox, who had one run in seven innings against Masahiro Tanaka
Joe Kelly left the bases loaded in the top of the ninth, catching a line drive back up the middle off the bat of Mason Williams.
“This game pretty much was on their side. It’s huge, man,’’ Ortiz said.
After Eduardo Rodriguez allowed four runs before being taken out in the third inning, the Sox bullpen kept the game close. Heath Hembree, Junichi Tazawa, rookie Robby Scott, and Kelly allowed one unearned run.
“Robby Scott set this one up. Three shutout innings were key,’’ manager John Farrell said. “Each guy who came to the mound did his job.’’
The Sox have won six of their last nine games.
Rodriguez came into the game with a 1.88 earned run average in six career starts against the Yankees. In two starts previously this season, he had allowed two runs over 14 innings.
That all went to pieces in the first inning.
Jacoby Ellsbury interrupted being booed with a single to center. Gary Sanchez, the rookie catcher who has sparked New York’s revival, followed with a double to center field. The ball landed at the base of the 420-foot sign in the triangle and bounced into the stands.
Ellsbury had to stop at third but that was only temporary as Starlin Castro singled to right field. Sanchez moved to third on the hit and scored on a sacrifice fly to center by Billy Butler.
Rodriguez stranded two runners in the second but could not get out of the third inning.
Castro doubled off the wall in left-center with one out and scored when Butler singled to center. Didi Gregorius followed with a double to center field. Butler went to third and scored when Chase Headley singled to right field.
Peter Abraham can be reached at pabraham@globe.com. Follow him on Twitter @PeteAbe.