BOSTON -- They were on the brink of a jubilant finish so many times. All the Red Sox needed was a walk-off, extra-innings hero. In the ninth, 10th, 11th and 12 innings, Boston put runners in scoring position, only to come away empty-handed.

And in the end, the missed opportunities were costly, both in the context of the game and in the race for the American League East.

Mike Timlin served up a two-out, three-run homer to Carlos Pena -- the pride of Haverhill, Mass. -- in the top of the 14th inning, paving the way for perhaps the most painful loss of the season for the Red Sox, a 4-2 defeat to the Rays.

Instead of winning the rubber match against the team they are trying to beat in the AL East, the Sox came up empty and fell 2 1/2 games behind. It was a bitter pill to swallow for the Red Sox, who had moved within a half-game of the lead after taking the opener of this series on Monday night.

The teams will meet again on Monday at Tropicana Field for the start of a three-game series.

Despite a second late-innings loss in a row to their division rivals, the Red Sox managed to stay upbeat.

"I'll take the effort," said catcher Jason Varitek. "We lost this game, but I'll take the effort and the opportunities we had. If we continue to play the game like we're playing the game, we'll be in good shape."

The crusher of it for Timlin was that the pitch might not have even been a strike. But Pena went the other way with the outside offering and nailed it over the Green Monster, just as he used to dream of doing during his youth as a Red Sox fan.

"I'm not sure how he lifted it," said Timlin. "I thought it was a popup and it went over the fence. What are you going to do? It was borderline. It was a possible strike, a possible ball. We weren't giving in. At that point, we weren't going to let him drive the ball out of right field. He had to go the opposite way."

With two outs and nobody on, Timlin surrendered a single to Akinori Iwamura and another one to Rocco Baldelli. That gave Pena a chance, and he followed through.

"We didn't want Pena to hit that inning, because we know what he can do," said Red Sox manager Terry Francona. "I don't think that was a bad pitch. To that point, so many people did so many things on both sides of the field."

And even after the dagger flew off Pena's bat, the Red Sox still gave themselves a realistic chance to turn the tables on the Rays in the bottom of the 14th.

Rays closer Troy Percival opened that 14th by giving up a double to Jacoby Ellsbury. Dustin Pedroia and David Ortiz followed with walks, loading the bases with nobody out.

Out came Rays manager Joe Maddon, and in came another reliever -- righty Jason Hammel.

Kevin Youkilis greeted him with a sacrifice fly to right, making it a 4-2 game, and moving Pedroia to third. But Jason Bay (0-for-7) struck out and Alex Cora flew out to center, and the game was over after five hours and two minutes.

The Red Sox left 16 men on base.

"We had a few lineouts in those situations too," said Varitek. "There were some balls hit well. They just didn't fall. It was just one of those things. The effort was there. Once again, we set the table there in the [14th] to give ourselves a chance."

In his second start back from the disabled list, Red Sox ace Josh Beckett allowed six hits and a run over six innings, walking two and striking out seven.

In what was a terrific night for Andy Sonnanstine (seven innings, four hits, one unearned run, seven strikeouts), the Red Sox were able to scratch something together in the third when Pedroia ripped an RBI double to left to bring home Jed Lowrie with the tying run.

Nobody could have ever imagined how long that tie would last.

Until Timlin's mishap, the Boston bullpen had a huge night, firing 7 1/3 shutout innings in relief of Beckett. Justin Masterson (two innings), Manny Delcarmen (two innings) and lefty Javy Lopez (2 1/3 innings) turned in particularly strong outings.

"Those guys threw the ball really well," said Timlin. "We'll go out there Friday, strap them on and keep attacking."

Still, the result was tough to swallow for Timlin.

"It's going to be tough," Timlin said. "It's going to be a long night for me. I feel like I let my teammates down. But I went out there and threw the ball, but I gave up the home run."

Though Tampa Bay remains up top in the division, the Red Sox are in good position to make the playoffs, holding a five-game lead over the Twins in the Wild Card standings. Boston has 17 games left in the season.

And until their rematch with the Rays, the Sox must deal with the red-hot Blue Jays for the next four games.

"It's not like it puts us out of the race by any means," said Timlin. "We just drop back one more game. But we're still in it quite a bit. We're not hanging our heads. Yeah, they beat us in this series, but we have a good team. We're going to do a great job down the road."