BOSTON — Orioles right-hander Miguel Gonzalez stifled the Red Sox for eight innings in the first game of a split doubleheader at Fenway Park, delivering arguably his best start of the season, but the Orioles still suffered a sobering, 3-2, walk-off loss to the Red Sox at Fenway Park.

Seemingly rested and recovered from their early-morning arrival in Boston on Friday after their scheduled afternoon game was postponed until Saturday because of rain, the Orioles received a boost from Gonzalez, but mustered just five hits on the afternoon.

Gonzalez battled through his deepest start in more than a year, but the Orioles lost on a pair of seeing-eye hits off left-hander T.J. McFarland in the bottom of the ninth, ending the Orioles’ four-game winning streak before an announced 35,714.

McFarland, who had an 0.67 ERA last month after allowing just one run over 15 2/3 innings, gave up a game-winning, broken-bat single to pinch hitter Jonathan Herrera with one out in the ninth.

As Herrera swung at a 1-0 fastball, the barrel of his bat twirled one way as the ball looped just over second baseman Jonathan Schoop’s glove and into shallow right-center field, scoring Jonny Gomes from second.

“What are you going to do?” Orioles manager Buck Showalter said. “I was real proud of the pitchers today, especially Miggy.”

The Red Sox by no means battered McFarland. He was brought into the game in the ninth to face two left-handed batters due up that inning but ended up facing three right-handers. Gomes, a pinch hitter, opened the inning with an infield single that J.J. Hardy backhanded deep in the hole at shortstop but couldn’t get to first in time.

David Ross then dropped a bunt that catcher Caleb Joseph corralled and looked to throw to second before settling for the sure out at first.

Herrera, a switch hitter, then sent the Orioles off the field with his walk-off hit. Adam Jones’ throw home was high and wide as Gomes slid into a Red Sox celebration at home plate.

The Orioles scored just two unearned runs off Boston left-hander Jon Lester in eight innings, managing five hits on the day. They went quietly in the top of the ninth as former Oriole Koji Uehara struck out Jones and Chris Davis in a perfect frame.

With the loss, the Orioles (46-40) blew an opportunity to go a full game up on the Toronto Blue Jays in the American League East heading into the doubleheader’s nightcap.

Gonzalez kept Boston’s bats at bay throughout the afternoon. After allowing two runs in the second inning, he retired 14 of the next 15 batters he faced.

“Just trying not to think too much and make my pitches,” Gonzalez said. “I was working down, so that was important. My [split-finger fastball] was good today, my slider — I thought everything was working well. Caleb did a good job back there by mixing pitches.”

As Gonzalez’s pitch count crept toward triple digits, Showalter stuck with him in the eighth. The right-hander stranded two runners in scoring position in his final frame after throwing his season-high 113th pitch of the day, one shy of his career best. His eight innings tied a career -high, last accomplished on June 11, 2013, against the Angels.

Gonzalez entered the eighth having allowed just two singles since the second inning, but hit Dustin Pedroia with a pitch and walked Mike Napoli with one out. He induced a pop-up in foul territory from A.J. Pierzynski for the second out, but a wild pitch moved both runners into scoring position.

Gonzalez struck out Xander Bogaerts looking on a 2-2, split-finger fastball to end the inning.

“That's the kind of Miggy we're going to need,” Showalter said. “He was solid. I thought he deserved a chance to finish off that eighth inning. He had some real low-duress innings. He was solid. Unfortunately, Lester was as good. And we were fortunate to be in it. They gave us a couple runs. I thought everybody threw the ball well. [McFarland] gave up a ground ball and a so-so bunt, at best, and a ball that broke the guy's bat in half.”

Gonzalez has posted quality starts in all four of his career starts against the Red Sox and is 3-0 with a 2.90 ERA in six career appearances against Boston.

“[I was] under control a little bit more,” Gonzalez said. “I told myself not to do too much, stay aggressive, down in the zone, especially against a team like this, that has a good lineup. We came up a little short. T.J.’s been throwing the ball really well. You can’t do anything about that. It’s baseball.”

Showalter said he went to McFarland — he also had left-hander Brian Matusz and right-hander Ryan Webb available in the bullpen — because he wanted to preserve some of his late-inning arms for a lead. McFarland also could give him some length if the game went to extra innings.