ST. PETERSBURG, Fla. — Of all the ways a game can end in extra innings, the walk-off error might be the most frustrating.

The Boston Red Sox literally dropped the ball on a potential third out twice on one play and the Tampa Bay Rays walked off with a 4-3, 11-inning win Wednesday night at Tropicana Field.

“It was awesome,” said Luke Maile, whose two-out double started the winning rally. “Even more fun than it looks on TV.”

With two outs and runners at first and second, Kevin Kiermaier hit a ground ball to first base, but Travis Shaw’s running flip to Heath Hembree (4-1) went off the reliever’s glove for an error. The subsequent throw home was in time to get Maile, but catcher Sandy Leon couldn’t hold onto the ball and the winning run scored.

“It’s kind of a routine 3-1 play,” Red Sox manager John Farrell said. “It ends up being the difference in this one, but yeah, that’s a routine play.”

The Rays had been 3-57 this season when trailing after seven innings, but they tied the score in the eighth on Evan Longoria’s 30th home run of the season.

Boston (71-55) held onto its share of first place in the American League East with Toronto’s loss to the Los Angeles Angels. Tampa Bay (53-72) was headed for its third consecutive loss before Longoria’s home run tied the score. It was the Rays’ third walk-off win this year.

Maile, who entered the game after the Rays pinch-ran for catcher Bobby Wilson in the ninth, got the rally started and took off for home as soon as he saw the ball on the ground at first.

“Once it kicked away, the pitcher and his momentum going that way, I knew I had a pretty good shot,” said Maile, who took a foul ball off his bare hand in the top of the 11th but was able to stay in the game.

The Rays got a leadoff single from Wilson in the ninth, and pinch runner Mikie Mahtook advanced to second with one out, but closer Craig Kimbrel got Kiermaier and Longoria on strikeouts looking to end the inning and strand the winning run at second base.

Neither team had a baserunner in the 10th. Brad Boxberger (2-0) got three strikeouts in the 11th, the last with Mookie Betts at second after the Boston right fielder walked and stole his 21st base of the season.

Before Longoria’s 30th home run, Boston’s David Ortiz got his 30th, becoming the oldest player in baseball history to reach 30 in a season. Ortiz, 40, also got to 100 RBIs with the home run, giving him 10 seasons of 30-plus home runs and 100-plus RBIs, the most ever for a Red Sox player.

Boston starter Rick Porcello was five outs from being the majors’ first 18-game winner before Longoria took him deep. Longoria has four seasons with at least 30 home runs, breaking Carlos Pena’s team record.

Porcello threw a career-high 123 pitches but was left with a no-decision. He allowed three runs on nine hits in 7 2/3 innings. He struck out eight without issuing a walk.

The Rays loaded the bases after Longoria’s home run, but reliever Brad Ziegler struck out Corey Dickerson to strand all three and preserve the tie.

The Red Sox took a potentially serious injury as rookie outfielder Andrew Benintendi had to be helped off the field after spraining his left knee as he landed badly in the seventh while running the bases. He was between second and third base when a ground ball was hit to Rays shortstop Matt Duffy, who turned and tagged him to start a double play.

Benintendi landed awkwardly on his left ankle. Chris Young replaced him in left field, and Benintendi will be evaluated Thursday.

“An unfortunate situation with a guy who’s really doing a great job for us,” Farrell said.

Wilson finished with three hits to lead the Rays, while Duffy and Logan Morrison had two hits apiece.

The Red Sox got two hits each from Ortiz, Betts and Dustin Pedroia.