BALTIMORE — What in the third inning looked like a laugher by the sixth had become a white-knuckler.

The Yankees’ bullpen, built for such important September scenarios, couldn’t quite close it out.

Manny Machado hit a walk-off, two-out, two-run homer in the ninth inning Tuesday night off Dellin Betances to send the Yankees to a stunning 7-6 loss in front of a crowd of 14,377 at Camden Yards, a gathering that waited out a rain delay of 2 hours, 14 minutes before the first pitch.

Few of them were left when Betances walked Tim Beckham with two outs in the ninth, then hung a 1-and-0 curveball that Machado drilled to left-center for the game-winner, his second homer of the night and 32nd of the season.

“He’s a great hitter, obviously,” Betances said. “When you leave a pitch like that to a guy like that, he’s going to usually punish it, and that’s what he did tonight.”

The Yankees (74-64) saw their winning streak end at three and, just as tough to swallow, arrived in the clubhouse just as the Red Sox topped the Blue Jays in 19 innings to push their lead back to 3 1⁄2 games in the AL East.

advertisement | advertise on newsday

A six-run third gave CC Sabathia a 6-1 lead but the lefthander, 11-5 with a 3.71 ERA this season, including 2-1 with a 1.89 ERA in his previous three starts, was victimized by the long ball.

An Orioles team that entered the night ranked second in the AL in homers with 212, hit three off Sabathia, the last of which, a two-run shot by Mark Trumbo in the sixth, brought Baltimore within 6-5. The O’s hit four overall.

“When we made mistakes,” Sabathia said, “they didn’t miss them.”

Tommy Kahnle took over for Sabathia with one out in the sixth and, though he walked a batter, got out of the inning. David Robertson pitched a perfect seventh and Aroldis Chapman did the same in the eighth.

Sabathia, 7-3 with a 2.81 ERA in 12 previous road starts this season, allowed five runs and eight hits in 5 1⁄3 innings.

Yankees videos

Orioles righthander Jeremy Hellickson allowed five runs (three earned), two hits and four walks in 2 1⁄3 innings.

Didi Gregorius and Jacoby Ellsbury both had two-run singles to highlight the six-run third that, at the time, seemed would be plenty. But Macha do’s 31st homer of the season in the bottom half brought the Orioles within 6-2 and Jonathan Schoop’s two-out blast, his 31st of the season, in the fifth made it 6-3.

Betances retired Welington Castillo on a flyout to start the ninth and struck out Pedro Alvarez. But he didn’t get the call on a 3-and-1 pitch to Beckham, the walk setting up Machado.

“I thought it was a strike,” Betances said. “But I have to be able to go out there and turn the page and get the next guy. Unfortunately tonight, I wasn’t able to do it.”