BALTIMORE — Coming off two straight shutout losses and precariously close to dropping out of serious playoff contention, the New York Yankees desperately needed a victory.

Using a starting lineup containing five rookies, manager Joe Girardi received precisely the performance he wanted.

Chase Headley and Austin Romine had two RBIs apiece, and New York beat the Baltimore Orioles 5-2 Sunday to prevent a three-game sweep and remain afloat in the AL wild-card race.

New York had won four straight series before losing 8-0 on Friday night and 2-0 on Saturday.

“You can’t afford to lose three in a row to these guys,” Girardi said. “We know they play really well in this building. We lost a tough one (Saturday) night. To be able to bounce back is important. It keeps us alive.”

Indeed, the young, retooled Yankees remain a part of the playoff picture, trailing Baltimore and Detroit by 3 1/2 games for the final wild-card spot.

“This was a big win. We needed it,” Romine said. “We just need to keep the rhythm moving into the next game and see if we can get on a roll.”

Luis Severino (2-8) pitched two strong innings of relief and Dellin Betances, the fifth New York reliever, got four outs for his eighth save.

The loss dropped Baltimore three games out of first place in the AL East.

“I mean, I’m trying to keep a positive mind frame,” first baseman Chris Davis said. “Two out of three at this point of the year. We won the series and we’ll move forward.”

The Orioles stranded nine and went 0 for 10 with runners in scoring position.

Asked if his team might have been flat after already clinching the series, manager Buck Showalter said, “We’re trying to win games. You beat them twice and you’re trying to beat them a third time. We just didn’t have much offensively.”

After falling behind 4-0 in the fourth inning, the Orioles closed to 4-2 before wasting an opportunity to complete the comeback in the fifth.

Adam Jones led off with a single and scored on a double by Pedro Alvarez. Severino replaced starter Michael Pineda and struck out Manny Machado before issuing two straight walks to load the bases. Steve Pearce struck out and Matt Wieters chopped a grounder to second base to end the threat.

Baltimore got only one more hit the rest of the way.

New York had been shut out three consecutive times by the Orioles over a seven-day span. “We’ve got to find a way to score some runs,” Girardi said after Saturday night’s loss.

The Yankees promptly put an end to the streak with a three-run first inning. Wade Miley (8-12) walked the first two batters and struck out the next two before Headley hit an RBI single. A wild pitch and a two-run single by Romine followed.

Headley singled home a run in the fourth, and Pearce hit a run-scoring grounder in the bottom half.

Brett Gardner made it 5-2 with a sacrifice fly in the ninth.

TRAINER’S ROOM

Yankees: SS Didi Gregorius was held out of the starting lineup after absorbing some bumps and bruises during the series. “His arm is sore where he’s been hit the past few days,” Girardi said.. . 1B Mark Teixeira doesn’t usually play in day games after playing the previous night, and thus was given Sunday off. Commenting on the ice pack Teixeira had on his neck before the game, Girardi said, “He deals with his neck, he deals with his knee. It’s not what you want, but it’s the reality.”

Orioles: RHP Chris Tillman (shoulder) threw off a mound and remains on course to come off the disabled list on Sept. 11.

UP NEXT

Yankees: RHP Masahiro Tanaka (11-4, 3.12 ERA) opens a three-game series against visiting Toronto on Monday. New York is 3-9 against the Blue Jays this season, losing all four series.

Orioles: RHP Ubaldo Jimenez (5-11, 6.46) starts Monday in the opener of a three-game series at Tampa Bay. Baltimore is 29-37 on the road, 3-3 at Tampa Bay.