NEW YORK The Orioles managed almost no offense Friday night. Their starting pitcher tied a career high with six walks. And yet they were close to winning.

But this is Yankee Stadium, where bad things often happen to the Orioles.

True to form Friday night, Carlos Beltran hit a two-out, three-run homer to left field off Orioles closer Zach Britton to give the New York Yankees a 5-3, walk-off win.

“You're one good pitch away,” Britton said. “Keeping the ball down, I can get out of that inning.”

It was Beltran's seventh homer of the season and the Yankees' first walk-off shot since last August.

Britton, who had allowed just one run in his previous 21 games and one hit in his previous five outings, gave up two singles and a walk before serving up the 96-mph fastball to Beltran on a 3-1 count.

Britton (3-1) hadn't lost yet this season and had blown just one other save in his first 10 chances. It was just his second homer allowed in 2014.

“It's a fine margin of error,” Orioles manager Buck Showalter said. “Tonight, he wasn't able to make the last pitch that [he has in] a lot of three-up, three-down [innings]. It's not an easy job. And he's been doing it at a very high level. And hopefully will again.”

The Orioles (37-35) have dropped two games behind the Yankees (39-33) for second place in the American League East.

The teams are 2-2 against each other at Yankee Stadium, where the Orioles have had a losing record in 15 of their past 16 seasons.

It was an all-around ugly game, but one the Orioles nearly won.

Right-hander Ubaldo Jimenez finished one out shy of picking up his second consecutive quality start  (three runs or fewer in six innings or more) — by definition, anyway.

But it was far from a quality outing from the Orioles' highest-paid pitcher.

Jimenez pitched into the sixth and allowed just one run, in the first inning. But he nearly handed over the game multiple times because of an inability to throw strikes.

He issued a season-high six walks in 5 2/3 innings, and on three different occasions loaded the bases without, somehow, yielding a run.

“I could look at the finished product and say one thing, but the six walks, I thought — he and we were very fortunate to survive those tonight,” Showalter said. “I think, more times than not, the baseball gods will get you on that one.”

Jimenez, whom the Orioles signed this offseason to a franchise-record four-year, $50 million free-agent deal for a pitcher, now has walked 16 batters in his past 14 innings, spanning three starts, and has walked 51 batters in 81 2/3 innings this season.

On Friday, Jimenez threw only 53 of his 104 pitches for strikes.

For the first half of the game, though, the storyline tilted toward the Yankees' 39-year-old right-hander, Hiroki Kuroda, who had a 1.67 ERA in five previous career starts against the Orioles in the Bronx. He continued that momentum Friday, holding the Orioles hitless through five innings — the third opposing hurler to do that to the Orioles this season.

In those previous two games — Kansas City's Danny Duffy threw 6 2/3 innings hitless May 17 and Boston's Brandon Workman had 5 2/3 hitless innings June 10 — the Orioles eventually broke up the no-hit bid but failed to score, losing, 1-0, each time.