The Yankees were sluggish early against the Orioles before their bats finally came to life.
Starlin Castro went 3-for-5 with a homer and three RBIs, leading the New York past Baltimore 7-4 in their series opener Monday.
New York maintained its lead atop the wild-card standings while dropping the Orioles two games back for the second spot pending Monday's later games. Didi Gregorius also homered for New York, which has won four of its past five games after taking three hours, 46 minutes to beat the Red Sox Sunday.
"They put together some really long at-bats and I thought Didi's homer really got us going," New York manager Joe Girardi said. "We were a little off the first time through the lineup. We were a little late at times. It seemed like they got into a good rhythm."
Yankees rookie Aaron Judge, who has 38 home runs, went 1-for-1 with four walks.
A fielder's choice by Brett Gardner and an RBI single by Castro in the sixth boosted New York's lead to 7-3. From there, Yankees reliever Chad Green (3-0) threw 2 1/3 scoreless innings with four strikeouts.
"Right now, I'm happy where I am," Green said. "When the offense puts up seven runs, it makes my job easier."
David Robertson entered in the eighth and walked the first two batters before getting out of the jam with a pair of strikeouts and a grounder.
Dellin Betances allowed a two-out, solo home run to Welington Castillo in the ninth before striking out Seth Smith to end the game. Castillo tied a career high with 19 home runs.
With a win Tuesday, the Yankees can take their first series at Camden Yards since 2013. The Orioles have gone 1-for-22 with runners in scoring position over the past two games.
"I'm feeling like we've had to scratch and claw for every run," Orioles first baseman Chris Davis said. "Just really battled till the last out. Anytime you're in this position as far as the postseason is concerned, it's not going to be easy. You just have to find a way to break through and come away with a win."
Tim Beckham gave Baltimore an early lead when he led off the first with a homer off rookie left-hander Jordan Montgomery. Davis went deep again with a two-run shot the following inning.
"I felt good," Montgomery said. "I had Beckham 3-1 and just threw a fastball in there to get back into the count and he put a good swing on it. I tried to settle in after that and keep us in the game. It was a good slider to (Davis). He just got it."
Castro got the Yankees' first hit off Dylan Bundy (13-9) with a leadoff single in the fourth. Gregorius followed with a two-run homer that cut the margin to 3-2.
That ended a streak of 12 scoreless innings for Bundy.
The Yankees tied the game later in the fourth inning on an RBI single by Jacoby Ellsbury. Castro gave the Yankees a 5-3 lead with another two-run homer that ended Bundy's day in the fifth.
Bundy was coming off his best month as a starter, going 4-0 with a 2.00 ERA in five starts in August. He struggled to carry that momentum and allowed five runs and five hits with five strikeouts and four walks.
"I felt the first couple innings I was getting away with stuff and missing my spots and they were hitting the ball right to people," Bundy said. "That's the type of lineup (they have) over there. They don't swing at too many pitches that are borderline pitches and they fouled off the good ones. Tough team."
NOTES: Baltimore OF Craig Gentry (fractured right finger) met with a hand specialist on Sunday. The Orioles hope he can rejoin the team by Sept. 12 in Toronto. ... Yankees C Gary Sanchez began serving a three-game suspension on Monday for his role in last month's brawl against the Tigers. Fellow C Austin Romine is still waiting to hear about his punishment for his involvement in that same fracas. ... Orioles SS J.J. Hardy (elbow) swung the bat on Monday after throwing the previous day.