Updated

The last of 12 walks cost the Tampa Bay Rays in a marathon loss.

Dustin Pedroia singled with two outs in the top of the 16th inning, snapping a scoreless tie and giving the Boston Red Sox a 1-0 victory over the Rays on Sunday night.

"I'd say this is probably the toughest loss of the year, to know that our pitching kept us in the whole game," Tampa Bay center fielder B.J. Upton said.

Pedroia lined a single to right field off Adam Russell (1-2), driving in Josh Reddick from third base. Reddick drew a leadoff walk, moved to second on a sacrifice bunt and to third on Marco Scutaro's infield hit.

"Anytime you have a leadoff walk it's a recipe for disaster," Russell said. "It just can't happen that late in a game."

Nine Tampa Bay pitcher combined for 12 walks and 16 strikeouts. Rays' hitters drew just one walk and fanned 13 times.

"It's never fun to lose a game like that," Rays manager Joe Maddon said. "You use a lot of pitchers and a lot of emotion, but I'm really proud of the way our boys handled tonight."

Maddon and bench coach Dave Martinez were ejected by plate umpire Chad Fairchild during the 11th inning. Maddon was tossed during a pitching change, leaving Martinez to lead the Rays. Martinez was ejected after leaving the dugout to have words with Fairchild after Scutaro slammed his bat to the ground in frustration after popping up.

The bat bounded toward catcher Kelly Shoppach, but didn't prevent him from making the catch.

The Red Sox improved to 12-2 since June 30, hiking the best record in the AL to 57-36. The third-place Rays have lost nine of 15 to fall a season-high seven games off the pace in the division.

"My takeaway is that we can beat the Red Sox and they know it," Maddon said. "My takeaway from the 1-0 loss to the Yankees the other day (last Sunday) is that we can beat the Yankees and they know it. We're not going away. It's just a temporary inconvenience right now."

Josh Beckett and five relievers combined to pitch a three-hitter for the Red Sox, who won for the eighth time in nine games, retaining a 1½-game lead over the second-place New York Yankees in the AL East.

Alfredo Aceves (5-1) pitched three scoreless innings for the win. Jonathan Papelbon worked the bottom of the 16th for his 21st save in 22 opportunities.

It was the longest 1-0 game in the major leagues since the Brewers at Angels on June 8, 2004, went 17 innings, according to STATS LLC. The longest 1-0 game ever was 24 innings between the Mets and Astros in 1968.

Both teams got outstanding performances from their starting pitchers, with Beckett allowing one hit — an infield single — and Tampa Bay's Jeff Niemann limiting the Red Sox's potent lineup to pair of singles through eight innings. They both also wasted opportunities to win it long before it ended after five hours, 44 minutes — the longest by time in Rays franchise history.

Beckett limited the Rays to one hit in a 3-0 complete game shutout at Tropicana Field last month, and the All-Star right-hander was equally tough on them this time.

Reid Brignac's third-inning, infield single was Tampa Bay's lone hit off Beckett on June 15. Evan Longoria's grounder back to the mound bounced off the pitcher's right foot with two outs in the first and was the only blemish on his pitching line Sunday as he struck out six and retired the last 22 batters he faced.

Niemann was almost as good while working a season-high eight innings, allowing a two-out single to Adrian Gonzalez in the first and Pedroia's single down the left field line leading off the fourth. The 6-foot-9 right-hander walked Reddick with one out in the fifth and Scutaro with two outs in the eighth, fanned a career-high 10 and didn't allow a runner past second base.

"Everything considered it was a great baseball game," Niemann said. "It's really unfortunate we came up on the short side of it.

Pedroia finished with three of the Red Sox's five hits. The rest of the team was 2-for-45.

Since spending six weeks on the DL with a lower back strain, Niemann has turned his season around with four strong performances over five starts since his return on June 20 against Milwaukee. In six starts prior to going on the DL, he went 1-4 with a 5.74 ERA.

NOTES: Tampa Bay RF Matt Joyce was scratched from the starting lineup because of a sore knee. The first-time All-Star was injured when he fouled a ball off his right kneecap during the fifth inning Saturday. He pinch hit in the ninth against Bard and lined out. ... The Rays didn't have a runner reach third. ... Tampa Bay C Jose Lobaton was placed on the 15-day disabled list after the game because of a sprained left knee. C Robinson Chirinos was recalled from Triple-A Durham.