By winning, the Rays (73-47) took the three-game series and are now 5-2 on their current 10-game, three-stop road trip. They improved to just two games under .500 on the road.
Carlos Pena led off the 12th and hit the first pitch from Santiago Casilla into the right-field stands for his 23rd home run of the season and a 6-5 Rays lead. Cliff Floyd followed with a double, and two outs later, he scored on Dioner Navarro's single to give the Rays a two-run cushion.
And they would need that cushion, as the A's fought back in the bottom half of the 12th. Oakland scored once before Rob Bowen mercifully grounded into a forceout, ending the game 4 hours and 24 minutes after it started.
"Hey, a great win," Rays manager Joe Maddon said. "I am so proud of the way our guys played today -- kept fighting, kept coming back. All the different things that occurred today -- truly what I'm looking for. I can't ask for anything more out of the position players, the pitchers, whomever. It's exactly what we're looking for here, and beyond everything else, the continuous maximum effort was tremendous."
The Rays got off to a rough start, as James Shields allowed a leadoff triple to Mark Ellis and, one pitch later, a home run to Carlos Gonzalez.
"It's part of the game," Shields said. "You move on."
Shields did, giving the Rays five innings. His teammates moved on as well, particularly Cliff Floyd.
The 14-year Major League veteran walked four times in six trips to the plate and scored four runs, twice coaxing his gimpy knees into high gear to score from first base thanks to Willy Aybar, who doubled in the second inning and tripled in the third.
"I pretty much emptied the tank today," Floyd said. "I haven't walked four times since Little League."
The Rays first appeared headed for a win in the ninth, when B.J. Upton's RBI double broke a 4-4 tie. His one-out hit came against Oakland rookie right-hander Brad Ziegler, a submarine-style pitcher who had not been scored upon in his Major League career, a period covering 39 1/3 innings.
But the A's tied the game in the bottom of the ninth when Jack Hannahan drew a leadoff walk before moving to second on a sacrifice bunt by Ellis. Troy Percival fielded the bunt and chased down Ellis, but sprained his right knee in the process and had to leave the game. Grant Balfour entered the game and struck out the first batter he faced before Frank Thomas connected for an RBI single to tie the game at 5.
The Rays shot themselves in the foot in the 11th. Rocco Baldelli singled to lead off the inning and went to third on a Ben Zobrist single. Then, Casilla uncorked a pitch that went to the backstop. Baldelli took off for home, but the ball ricocheted back to catcher Kurt Suzuki, who turned and tagged Baldelli out at the plate. Akinori Iwamura grounded out and Upton lined out to end the inning.
Following Balfour's departure, Jason Hammel entered the game and managed to give the team 2 2/3 innings without allowing an earned run, picking up his fourth victory of the season.
But the win didn't come without a few more anxiety-filled moments. Jason Bartlett entered the game at shortstop in the bottom of the 12th and immediately booted Emil Brown's grounder. Bobby Crosby then singled to left before Hammel threw a wild pitch, which allowed Brown to advance to third. Just when it appeared the Rays would lose the lead, Kurt Suzuki hit a sharp grounder to Bartlett, who promptly converted the chance into a 6-4-3 double play.
"That was awesome," Hamel said. "Pitcher's best friend, it kept us in it."
One run scored, and Trever Miller took over from Hammel to became the eighth hurler used by the Rays. Only once in club history (Sept. 27, 1998) had the Rays used that many pitchers in a game.
Miller hit the first batter he faced before getting a game-ending groundout to claim his first save of the season and eighth of his career.
Thursday's game, no matter how long -- or, at times, ugly -- was savored in the visitors' clubhouse as the Rays readied to board a plane to Texas, where they will play the final three games of their 10-game trip.
"We believe that as long as we're on that field, we've got a chance to win that ballgame," Floyd said. "I just think it's us believing in ourselves no matter who we have in the field, and go out there and doing it."
Bill Chastain is a reporter for MLB.com. This story was not subject to the approval of Major League Baseball or its clubs.