Updated

Chris Davis' two-run single in the 12th inning was the difference, as Baltimore outlasted Boston, 5-3, in the middle test of a three-game series.

Matt Wieters doubled twice and drove in two, while Brian Roberts added two hits and an RBI for the Orioles, who have taken three in a row and four of five.

T.J. McFarland (2-1) tossed a scoreless 11th and Jim Johnson did the same in the bottom of the 12th for his 47th save as Baltimore crept within a game of Texas for the final AL wild card spot as the Rangers fell in extra innings to Tampa Bay.

David Ortiz hit a two-run shot and Mike Napoli added a solo blast for the Red Sox, who could have at least clinched a playoff berth with a victory coupled with the Indians' loss to Kansas City.

Boston's magic number for clinching the AL East remained at three, after the Rays' victory.

Franklin Morales (2-2) was charged for three hits and the winning runs over two relief innings.

In the 12th, J.J. Hardy hit a one-out single and moved up on a Roberts base hit. Both men advanced on a wild pitch and pinch-hitter Steve Pearce was walked intentionally to load the bases, but Manny Machado fouled out. Davis' single up the middle scored two and provided the Orioles with a 5-3 edge. Morales recovered to pick Davis off at first, but the damage was done.

"Anytime you're in a game like that, you want to be the guy that gets the hit," said Davis. "Whether it's a squirter up the middle or a home run or whatever it is."

Johnson gave up a leadoff hit to Jarrod Saltalamacchia, who was erased on a fielder's choice grounder by Dustin Pedroia, and Pedroia was thrown out at second on a Jackie Bradley dribbler back to the mound. Ortiz then grounded to second to end the contest.

Ortiz reached the seats in right with one down in the home first, one batter after Shane Victorino was aboard thanks to a single.

Boston loaded the bases on an infield hit, bunt single and base hit that reached the outfield to start the third, but no runs resulted as Napoli lined out to short and Jonny Gomes hit into an inning-ending double play.

Danny Valencia singled to start the fifth and scored when Wieters doubled to center, and the O's catcher crossed the plate two batters later on Roberts' double.

Nick Markakis kept the Baltimore sixth alive with a single, moved up on a Valencia base hit and scored on Wieters' ground-rule double over the low wall in right to give Baltimore a one-run edge.

Napoli's blast to center to kick off the home half tied the game, 3-3, but the Sox eventually stranded two against Francisco Rodriguez later in the frame.

Boston left two on in the 11th, as Stephen Drew hit into a 4-6-3 double play.

"I felt like we hit a lot of balls hard, I think it was 17 hits or something but we just couldn't get that big hit," said Red Sox catcher David Ross.

Game Notes

The Orioles played in their 114th errorless game of the season, eclipsing the 2008 Houston Astros for the most in the majors in the modern era ... Baltimore starter Wei-Yin Chen gave up 11 hits and three runs while striking out five in 5 2/3 innings, while Boston's Jake Peavy worked seven innings and allowed six hits and three runs, fanning eight ... The Red Sox fell to 10-6 in extra innings this season ... Victorino left the game with a jammed thumb ... Sox fans were treated to "Dollar Beard Night," where any customer with a beard -- real or otherwise -- attended the game for $1.