ST. PETERSBURG, Fla. – The Tampa Bay Rays clinched the American League wild card with a stunning rally Wednesday night, overcoming a late seven-run deficit and then beating the New York Yankees 8-7 on Evan Longoria’s home run in the 12th inning.

The Rays’ win came four minutes after Boston blew a one-run lead in the ninth at Baltimore and lost 4-3, causing a roar at Tropicana Field.

The Red Sox, who held a nine-game lead over the Rays in early September, and Tampa Bay began the final day of the regular season tied for the wild card.

Longoria hit a three-run homer in the eighth that capped a six-run burst. Pinch-hitter Dan Johnson’s two-out, two-strike solo homer in the ninth tied it for Tampa Bay.

Longoria won it with a one-out shot barely inside the left-field foul pole, finishing the comeback from a 7-0 margin.

Tampa Bay will open its best-of-five playoff matchup at Texas on Friday. The Yankees will host Detroit in Game 1 Friday night.

Longoria connected off Scott Proctor (0-3) to end it. Proctor was the Yankees’ 11th pitcher of the game — they did not use all-time career saves leader Mariano Rivera, and Johnson hit his tying shot off Cory Wade.

The Yankees pulled their regulars throughout the game and finished with a lineup that looked more like a Triple-A team.

The Rays escaped a first-and-third, no-outs jam in the 12th. Longoria made the key play, fielding a grounder at third and making a snap tag to catch the runner diving back to the bag.

Mark Teixeira hit a grand slam off David Price and a solo homer as the Yankees built their big lead. His second-inning slam put the Yankees ahead, 5-0.