ST. PETERSBURG, Fla. — The Boston Red Sox still got the win. It just turned out to be much more complicated than expected.

RBI singles by Daniel Nava and Jarrod Saltalamacchia in the 14th inning finished off a game the Red Sox by all appearances had put away in the first inning, and Boston outlasted the Tampa Bay Rays 10-8 at Tropicana Field on Monday.

The deciding runs came against Cesar Ramos (1-2), the Rays’ eighth pitcher and the first in club history to throw three innings on consecutive nights.

Franklin Morales (2-0) pitched 1 1/3 scoreless innings to earn the win. He had been scheduled to start Wednesday.

In the longest game of the season for both teams at five hours, 24 minutes — and the second-longest in Rays history — the American League East-leading Red Sox (40-25) extended their lead over the fourth-place Rays to five games.

Ben Zobrist tied the Tampa Bay single-game record with five hits (in seven at-bats).

Red Sox manager John Farrell saw many positives despite the blown lead. His beleaguered bullpen held up well enough, but rosters moves will be required “to get some arms in here” short-term, he said, and second baseman Dustin Pedroia saved runs twice with sparkling inning-ending defensive plays.

Saltalamacchia went 4-for-7 with three RBIs while catching all 14 innings.

“We did a lot of good things well tonight,” Farrell said, “most importantly scoring one more run than they did.”

The Rays stranded eight runners in scoring position, three of them at third base.

“We had a chance. We had a definite chance,” Rays manager Joe Maddon said.

Saltalamacchia’s two-run single with two outs in the top of the 10th inning off Fernando Rodney gave Boston an 8-6 lead and appeared to bail out the Red Sox after they squandered the 6-0 lead they grabbed in the top of the first.

However, Tampa Bay responded again with a solo home run from Jose Lobaton and a bases-loaded walk to Kelly Johnson by closer Andrew Bailey. However, the Rays got nothing more from a bases-loaded opportunity with no outs, as Evan Longoria hit into a 5-2-3 double play and pinch hitter Sam Fuld’s bunt attempt was unsuccessful.

Tampa Bay fought back from the early deficit to tie the game at 6 in the eighth on a wild pitch from Junichi Tazawa. Yunel Escobar, who doubled leading off the inning and took third on a groundout, slid under the pitcher’s tag on a relay throw from Saltalamacchia. Zobrist followed with a double and nearly scored with two outs when Tazawa misread a pop-up over the mound, forcing Pedroia to race in for a sliding catch behind him.

The first six Red Sox batters of the game scored as Boston battered Rays starter Alex Cobb. Boston produced three doubles in the inning, the first a deflection off first baseman James Loney’s glove on a Shane Victorino drive that sent Jacoby Ellsbury — on with a leadoff single — to third. Pedroia singled home Ellsbury and Victorino for a 2-0 lead before David Ortiz walked. Nava upped the lead to 3-0 with a double high off the left field wall. Mike Carp singled in two more for a 5-0 lead. Will Middlebrooks added another run with a single.

Cobb required 37 pitches to record the first out on an infield popup to short by Stephen Drew. His 38th pitch ended the inning on a double play.

Cobb settled down thereafter, but he was pulled after four innings when his pitch count reached 98. One start after blanking the major league’s leading scoring offense at Detroit for 7 2/3 innings, he was charged with six earned runs on seven hits and three walks.

The lead didn’t seem big enough for much of John Lackey’s unsteady 5 2/3 innings. Tampa Bay immediately trimmed two runs from the deficit in the first as leadoff hitter Matt Joyce and Longoria each hit their 11th home runs of the season. Lackey labored with the lead again in the second, allowing three straight singles, but Tampa Bay produced just one run on a Luke Scott groundout.

The first two Rays batters of the third inning singled, also, however, and Zobrist narrowed the score to 6-4 with a run-scoring single. Lackey retired Longoria with the tying run on second to escape again.

“They took advantage of me trying to throw strikes,” Lackey said. “They were swinging at everything. Give them credit, though.”

Lackey cruised through the fifth but had an eventful sixth, being struck on the right foot by a Lobaton grounder (the putout was made nonetheless) and hitting Joyce in the back with a two-out pitch that prompted benches to clear briefly. The clubs have had an acrimonious relationship dating back several years, including a 2008 brawl at Fenway Park.

Craig Breslow entered to strike out Johnson and preserve the 6-4 lead.

Lackey was charged with four earned runs on 10 hits. He has allowed three earned runs or fewer in eight of 10 starts this season, the two exceptions being against the Rays.

Loney’s eighth homer of the season, off Andrew Miller, cut the margin to 6-5 in the seventh.

NOTES: Lackey said he was “just trying to take back some of the plate for me,” when he hit Joyce but denied intent. Joyce called it a “pretty bush-league move.” … If the Red Sox had lost, it would have been their first loss after having a six-run lead in the first inning since 1981. … The longest game in Rays history was also against Boston. … Tampa Bay LHP David Price, defending American League Cy Young Award winner, said he felt the best he has all year after throwing a 45-pitch bullpen session on Monday that included curveballs. He was placed on the 15-day disabled list May 16 due to triceps tightness. Maddon said Price would make three minor league rehab starts after throwing another bullpen session Wednesday and a simulated game Saturday. … Middlebrooks returned from the 15-day disabled list and was inserted into the eighth spot in the lineup. He finished 1-for-7. Middlebrooks was batting .201 with eight homers before sustaining a lower back strain. Farrell said INF Jose Iglesias would remain with the team in a utility function after batting .435 and crafting a 13-game hit streak — the longest by an American League rookie — in Middlebrooks’ absence. INF Pedro Ciriaco was designated for assignment. … Pedroia went 1-for-5, extending his hitting streak to 14 games.