BALTIMORE – The Boston Red Sox put a satisfactory finish on a potentially disastrous trip Thursday night.

Adrian Beltre homered to spark a five-run second inning, and the Red Sox beat the Baltimore Orioles 6-4 to win the three-game series.

David Ortiz drove in two runs and J.D. Drew had three hits for the Red Sox, who moved within 6½ games of idle Tampa Bay in the AL wild-card race.

Boston dropped 2 of 3 on the road against the Rays, then lost Tuesday night in Baltimore before winning two straight.

The injury-riddled Red Sox still have plenty of ground to make up, but at least they’re still in the hunt.

“We got off to a rough start on the road trip, but we were able to finish strong, win these last two,” said Scott Atchison, who pitched two scoreless innings of relief.

“We got some momentum going and hopefully we can take it home, get on a big roll. You never know what can happen from there.”

The Red Sox open a six-game homestand today against former teammate Manny Ramirez and the Chicago White Sox.

“We have to play some more games and keep winning,” catcher Victor Martinez said.

After missing his last start with a sore back, Daisuke Matsuzaka (9-4) gave up four runs in 5 2/3 innings. He has allowed four runs in each of his last four outings.

“Right now it’s not about me or my performance,” the right-hander said through an interpreter. “It’s the fact that the team can get the win in the games that I start.”

Jonathan Papelbon worked the ninth for his 35th save, and second in two nights. Baltimore got runners on second and third with one out before Papelbon struck out Cesar Izturis and rookie Josh Bell.

Coming close wasn’t good enough for Orioles Manager Buck Showalter, denied a second time his 900th career win.

“I am proud that we had a chance, brought the go-ahead run to the plate,” he said. “You’d like to finish it off. That’s where the satisfaction comes in.”

Papelbon is the only pitcher in major-league history to record at least 35 saves in each of his first five seasons. In 38 career appearances against the Orioles, he has 30 saves and an 0.75 ERA.

Down 5-0 and limited to two hits over the first five innings, the Orioles chased Matsuzaka with a four-run sixth. Nick Markakis hit an RBI single and Ty Wigginton followed with a run-scoring groundout before Matt Wieters hit a two-run double.

“It was the first time since I aggravated my back that I threw at full strength. I think there’s a little adjustment there that I need to get over,” Matsuzaka said. “In the sixth inning I think I rushed a little bit.”

A poor fielding play was a factor in the pivotal second inning. After Beltre hit his 25th homer on Brad Bergesen’s first pitch, the Red Sox got another run when Wigginton mishandled a grounder by Daniel Nava. Ryan Kalish added an RBI double before Ortiz slapped a two-run, opposite-field single to left.