BOSTON (AP) -- The Baltimore Orioles did plenty to hurt their chances despite Chris Davis' team-record 51st homer.
The best fielding team in the majors committed three errors, Scott Feldman walked a season-high six batters and they had to face Boston closer Koji Uehara, who had retired his previous 37 hitters.
But Danny Valencia ended that streak with a triple to lead off the ninth, Matt Wieters' hit a go-ahead sacrifice fly and the Orioles beat the Red Sox 3-2 on Tuesday night to stay two games behind in the AL wild-card race.
''They were real frustrated that we didn't make a couple of plays that we normally made,'' Baltimore manager Buck Showalter said, ''but the bullpen was really good. Scotty bent but didn't break.
''You don't like your chances there with Koji, but we got a big hit from Danny.''
Despite the loss, Boston's magic number to clinch the AL East dropped to three when Tampa Bay lost to Texas 7-1.
The Orioles overcame a 2-0 deficit on Brian Roberts' run-scoring groundout in the fifth and Davis' homer in the sixth that tied the score 2-2 and broke the club record set by Brady Anderson in 1996.
''I'm excited,'' Davis said. ''I've said it all along. When you're hitting homers and your team's not winning it feels like it's kind of for nothing. So I was obviously glad to tie the game.''
The homer came off starter Ryan Dempster, who allowed three hits in six innings.
''He just lifted that ball and got it up in the air - way, way, way up in the air - and it just kept carrying,'' Dempster said.
Uehara (4-1) fell four outs short of Bobby Jenks' major league record for a reliever of 41 consecutive retired batters set in 2007 and Mark Buehrle's mark for all pitchers of 45 in 2009. The run was the first off Uehara in 30 2-3 innings and ended a streak of 27 scoreless outings since July 9.
''I wasn't aware of it,'' Valencia said, ''but I'm not surprised by it because he's got great stuff.''
Valencia led off the ninth with a triple to center and pinch-runner Alexi Casilla scored easily on Wieters' fly to right.
Tommy Hunter (6-4) got the win with a perfect eighth. Jim Johnson got his AL-best 46th save.
''Koji's been so good for us,'' Boston manager John Farrell said. ''Even after the run allowed, he continued to pitch as he has. It didn't faze him and he finished out the inning.
The Red Sox managed just three hits, a leadoff homer by Dustin Pedroia in the first, a double by Mike Carp in the second and a single by Jarrod Saltalamacchia with one out in the ninth.
The Orioles threatened in the eighth when Nate McLouth singled and took third on Manny Machado's AL-leading 51st double with no outs. But Craig Breslow replaced Brandon Workman and needed just six pitches to get out of the jam. With the infield in, he got Davis and Adam Jones to ground out to shortstop Stephen Drew as the runners held then retired Nick Markakis on a fly to left.
The Red Sox took a 1-0 lead on Pedroia's ninth homer of the season. Left fielder McLouth didn't even move as it went deep into the seats above the Green Monster.
They made it 2-0 in the fourth with the help of two errors by a team that had made just 43 all year.
With one out and third baseman Machado playing in short right field on the shift for Saltalamacchia, he charged a hard grounder and let it get by him for an error. Drew then walked before a double steal put runners at second and third. Xander Bogaerts followed with a low liner to left field that McLouth charged. The ball hit off his glove for an error and Saltalamacchia scored. Bogaerts was credited with a sacrifice fly.
Machado made a throwing error in the seventh on a grounder down the line by Pedroia.
The Orioles made it 2-1 in the fifth when Valencia walked, took third on a double by J.J. Hardy and scored on a groundout by Roberts.
NOTES: The Red Sox stole three bases and have been successful in their last 35 attempts. ... Baltimore's total of errorless games remained at 113, tied with Houston's total in 2008 for the most since 1900. ... Wei-Yin Chin (7-7) pitches for Baltimore against Jake Peavy (3-1) in the second game of the three-game series Wednesday night. ... The Red Sox recalled IF Brock Holt, and RHPs Brayan Villarreal and Steven Wright from Triple-A Pawtucket.