BOSTON — The Boston Red Sox blew a four-run lead.

Then they erased a three-run deficit.

Finally the Red Sox lost, again.

The Tampa Bay Rays enjoyed the last rally, beating the Boston Red Sox 12-8 on Thursday at Fenway Park.

It was the final game of a 10-game homestand, which found the Red Sox going 4-6, including two losses to the weak-hitting Rays.

With two outs in the eighth inning, Steve Souza smacked an RBI double just over the outstretched glove of Jackie Bradley Jr., against the wall in left-center, breaking an 8-8 tie.

The Rays tacked on three runs in the ninth.

“That was a resilient win,” Rays Manager Kevin Cash said.

Right-hander William Cuevas, an All-Star with the Portland Sea Dogs last year, took the loss in his major league debut. He allowed two runs over 21/3 innings.

Dustin Pedroia and Mookie Betts homered for Boston.

Red Sox ace David Price couldn’t hold a 5-1 lead after the first inning. Price lasted only 3 2/3 innings, allowing eight runs on eight hits, two walks and two hit batters. Price didn’t factor into the decision but his ERA ballooned to 7.06 in four starts.

Meanwhile, Rays starter Jake Odorizzi rebounded from a 37-pitch first inning to last into the fifth.

Odorizzi looked in big trouble in the first, giving up five runs – and it could have been worse.

Mookie Betts singled to lead off and Pedroia launched his first home run, over the Green Monster. Xander Bogaerts doubled and David Ortiz crushed a line drive that right fielder Souza made a highlight, running catch on.

Three more hits (Hanley Ramirez, Travis Shaw and Brock Holt) and a double steal (Shaw/Holt) brought in three runs for a 5-1 lead with one out. Christian Vazquez grounded into a fielder’s choice and Bradley struck out to end the inning.

“To jump out to a (5-1) lead, we feel like we’re in pretty good shape,” Manager John Farrell said.

But Price never looked in sync. His pitch count ran to 42 in the second inning, and 64 in the third, when former teammate Evan Longoria got his first hit off Price, a home run to left center, closing the score to 5-2.

Price still had five strikeouts through three innings but lost it in the fourth, retiring only one of the seven batters he faced, including a two-run homer by Curt Casali. He left with the score 7-5, with a runner on second, who scored on Souza’s single against Matt Barnes.

“I felt good in the bullpen,” Price said. “That’s the best I’ve warmed up in my four starts. To not have that translate into the game is frustrating.”

Odorizzi left after allowing a leadoff double to Bogaerts in the fifth. Reliever Enny Romero cruised but gave up a walk in the sixth.

With two outs, Danny Farquhar relieved and Betts swatted his fourth home run of the season, closing the score to 8-7.

Shaw doubled in Ortiz in the seventh to tie the game. The Red Sox loaded the bases with one out, but both Bradley and Betts struck out.

In the top of the eighth, Cuevas issued a one-out walk. After a fielder’s choice, Souza came up, worked the count to 3-1 and jumped on a fastball, scoring Desmond Jennings from first for a 9-8 lead.

Cuevas allowed a double in the ninth and left with one out.

Noe Ramirez gave up two hits and hit two batters, and the Rays piled it on.

NOTES: When Cuevas struggled with a 2-6 record and 4.70 ERA in advanced Class A Salem in 2014, his career looked stagnant. But on Thursday, there was Cuevas standing in front of his Fenway Park locker. Cuevas, 25, turned his prospects around last year in Portland (8-5, 3.40, All-Star selection), reached Pawtucket and got the call to the majors Tuesday. “I was so excited, I couldn’t sleep,” he said. “It’s incredible how quickly it happened. You never know when so you have to be ready.” Cuevas got into Thursday’s game and took the loss. With Boston needing more fresh arms from Pawtucket, Cuevas was informed after the game that he’s returning to Triple-A for now.

Boston doesn’t have a starter listed for Sunday (with Joe Kelly hurt), but Henry Owens seems the likely choice, and even more so after Brian Johnson started for Pawtucket on Thursday. … Reliever Carson Smith pitched in an extended spring training game in Fort Myers, Florida, and will so again Saturday. Smith, who is on the disabled list with a strained forearm, will likely begin rehab assignments next week (the Sea Dogs will be on the road next week until Friday) … Thursday’s nine-inning game lasted 4 hours, 16 minutes. Compare that to the Sea Dogs’ 17-inning game last Friday, which lasted 4 hours, 33 minutes.