NEW YORK (AP) Trailing by four runs in the ninth inning, the somnambulant New York Yankees were on the verge of falling 10 games behind division-leading Baltimore in the AL East. Many in the crowd of 39,875 already had streamed out, convinced they had seen another listless loss in a season filled with them.
Those who left early missed an astonishing comeback the Yankees hope sparks the turnaround that has eluded them.
New York's first three batters reached, cutting the deficit by a run, and an electric seven-pitch span unfolded.
Brian McCann hit a tying, three-run homer off Sam Dyson, Starlin Castro walked and Didi Gregorius capped the six-run inning with his two-run shot, lifting the Yankees to a 9-7 win over the Texas Rangers, the team with American League's best record.
''Probably the biggest win of the year for us,'' Alex Rodriguez said.
New York trailed 7-2 Wednesday night and had a 1.2 percent win probability, according to Fangraphs, before McCann's solo homer with one out in the eighth off Cesar Ramos. McCann, bothered by patellar tendinitis, grabbed his left knee while rounding first and limped as he circled the bases.
Matt Bush came in and escaped a two-on jam, and the 30-year-old rookie, seeking his first big league save, was left in for the ninth.
Win probability was up to only 1.8 percent before Rob Refsnyder singled leading off the ninth. Jacoby Ellsbury followed with a walk, and Dyson (1-2) relieved.
New York had been 0 for 7 with runners in scoring position before Brett Gardner singled, and Refsnyder came home when center fielder Ian Desmond bobbled the ball for an error.
After Rodriguez lined out, McCann lined a 97 mph, 1-0 fastball on the inside of the plate above the knees and sent it about six rows deep into the right-field seats. Castro walked on five pitches, and Gregorius drove Dyson's first offering, an outside changeup at the knees, into the first row above the right-field scoreboard, where the ball ricocheted off a fan's glove and back onto the field.
''I didn't know it was gone like right away. After I stepped on first, that's when I saw it,'' Gregorius said. ''I didn't smile because I didn't want to smile, but as soon as I rounded third, see all the guys there, everybody cheering, I had to smile right there.''
It was the first game-ending homer of Gregorius' career; he said his only other one was for the Netherlands national team in October 2010 against Taiwan in the Intercontinental Cup.
''Threw some quality pitches down in the zone and they got hit. That's the end of it,'' said Dyson, who blew a save for the second time in 18 chances. ''I kind of came in and let everybody down.''
New York (38-39), which has never been more than two games above .500 this year, had lost the series opener, wasting a ninth-inning lead after a 3 1/2-hour rain delay in a game that ended at 2:44 a.m. Cole Hamels shut down the Yankees on Tuesday, extending their losing streak to three.
''There's a sense of urgency,'' manager Joe Girardi said before the game. ''As days tick away, you start to run out of time, and you don't want to bury yourself.''
Texas opened a 5-1 lead in the third against Masahiro Tanaka when Nomar Mazara, a 21-year-old rookie who is the youngest position player in the majors, hit a three-run double and scored on Prince Fielder's single off a diving Refsnyder at first.
Rougned Odor added his 15th homer in the sixth, an opposite-field drive to left, and Adrian Beltre hit an opposite-field homer to right in the eighth off Luis Cessa (1-0), who allowed one run in three innings for his first big league win.
Chase Headley had homered in the second against Nick Martinez, who played college ball in the Bronx with Fordham, and had a sacrifice fly in the sixth off Ramos. The drama was yet to come. New York had not won this year when trailing after eight innings.
''It can be huge,'' McCann said. ''We dug ourselves a hole early in April, and we're getting out of it right now.''
Gregorius hoped the comeback was just a prelude.
''I would say the biggest is yet to come,'' he said.
Before McCann, the previous Yankees player to homer in the eighth and ninth innings of a game was Tony Clark in 2004, according to the Elias Sports Bureau. ... The Yankees had not won when trailing by four runs in the ninth inning or later since beating Oakland 10-9 on Sept. 22, 2012, when they allowed four runs in the 13th, tied the score in the bottom half and prevailed 10-9 in the 14th.
Yankees: After leaving Tuesday's game in the first inning with a tight right hamstring, Carlos Beltran probably won't be available through at least Thursday, according to Girardi.
Yankees RHP Michael Pineda (3-7) starts Thursday's series finale against RHP A.J. Griffin (3-0). Pineda is 1-1 with a 3.00 ERA in his last four starts. Griffin returned from a stint on the DL caused by shoulder soreness and allowed two runs over 4 1/3 innings against Boston last Saturday.