Evan Longoria sparks pandemonion among Rays fans by hitting the winning home run.

Story highlights

The Tampa Bay Rays and St. Louis Cardinals have clinched MLB wild card berths

The Rays came back from 7-0 down to win in the 12th-inning

The Cardinals beat Houston 8-0 to earn their playoff berth

Boston Red Sox and Atlanta Braves suffer late-season collapses to miss out

CNN  — 

The Tampa Bay Rays and St. Louis Cardinals won wild card berths in the Major League Baseball (MLB) playoffs after dramatic victories Wednesday.

Trailing 7-0 to the American League (AL) East champion New York Yankees in St. Petersburg, Florida, the Rays launched an astonishing comeback to win in the 12th-inning.

Despite their comeback success, the Rays still needed the Baltimore Orioles to defeat the Boston Red Sox in Maryland to clinch the berth on the regular season’s final night. The O’s obliged by notching a 4-3 win in the bottom of the ninth in a game delayed by rain for an hour and a half.

There was similar drama in the National League (NL), as the Cardinals capitalized on the Atlanta Braves’ 4-3 loss to National League East champion Philadelphia. The Cardinals beat Houston 8-0 to earn the other wild card spot.

Major League Baseball introduced the wild card system in 1994. It rewards the two best-performing second-place teams with a spot in the playoffs – with one team coming from each league.

The Rays’ victory capped a remarkable turnaround, with the Florida-based team having been nine games behind the Red Sox on September 3.

Third baseman Evan Longoria hit the game-winning home run in the 12th-inning, and told the Rays’ website it was like something “meant for some kind of book.”

“If you could have drawn it up any better, it would be completely fake,” he said.

The Cardinals completed an equally impressive comeback, having trailed the Braves by 10 games on August 25.

“Once we got within hailing distance, the game of baseball is what it is,” Cardinals manager Tony La Russa said on the team’s website. “We kept doing things that gave us a chance.”

“A winning record against winning clubs. We just kept cranking out extra wins, and it made the thing look better and better.”

After Wednesday’s events, the line-up for the 2011 MLB playoffs has now been decided.

The Rays’ reward for their heroics is a series against the AL West champion Texas Rangers, while the Yankees will go up against the AL Central-winning Detroit Tigers.

In the National League, the Cardinals will take on the Phillies, with the Arizona Diamondbacks and the Milwaukee Brewers meeting in the other National League Division Series.

CNN’s Matt Cherry contributed to this report