ST. PETERSBURG, Fla. -- Down by nine games in early September, down by seven runs on the final day of the season.

And yet somehow, these Tampa Bay Rays made it to the playoffs.

Evan Longoria capped a startling comeback by hitting his second home run of the game, hooking a drive barely inside the left-field foul pole in the 12th inning Wednesday night and giving the Rays the AL wild-card spot with an 8-7 win over the New York Yankees.

Pinch-hitter Dan Johnson saved the Rays with a two-out, two-strike solo home runs in the ninth that made it 7-all.

Longoria connected shortly after midnight, four minutes after Boston blew a ninth-inning lead and lost at Baltimore 4-3. The Red Sox, who seemed to be running away from the Rays a few weeks ago, and Tampa Bay began the day tied for the final AL playoff spot.

"It's a good feeling knowing that one put us into the playoffs and not into a playoff game," Longoria said. "So I'm just thinking about, 'Wow, did this just really happen?' ... Man, when I saw it clear the fence it didn't seem real."

The crowd of 29,518 at Tropicana Field was mostly silent as the Rays trailed 7-0 going into the eighth. But Longoria's three-run homer finished a six-run burst in the eighth and Johnson, hitting only .108, tied it with a shot off Cory Wade.

The fans roared when the Red Sox loss was posted on the scorecard. Moments later, they erupted again when Longoria homered.

"It's a storybook finish," designated hitter Johnny Damon said. "Definitely gratifying."