The Orioles trailed by six runs going into the seventh inning against the San Francisco Giants on Sunday, nine outs away from pondering a disappointing road trip on a long flight back to Baltimore.

The game's outcome, however, turned in the final innings, as the Orioles scored seven unanswered runs to beat the Giants, 8-7, at AT&T Park. The late-inning hero was a familiar one: second baseman Jonathan Schoop, who completed the comeback with a three-run homer off Giants closer Santiago Casilla in the ninth.

“This team never quits,” Schoop said. “That's why when we go in there, we say, ‘Let's chip away, let's keep grinding, let's keep battling,' because this team, from one to nine, can do damage. If we put good at-bats together, we'll score some runs.”

The Orioles (66-51) salvaged their stay in the Bay Area, taking two of three from San Francisco after losing three of four to the last-place Oakland Athletics. They remained a half-game out of the American League East lead.

Left-hander Wade Miley struggled through the shortest of his three starts with the Orioles, allowing 10 base runners (eight hits and two walks) through 41/3 innings. Miley, acquired at the nonwaiver trade deadline, was charged with seven runs — six earned — and now has a 7.04 ERA in three starts with the Orioles.

The Orioles bullpen didn't help him. Right-hander Tyler Wilson allowed all three inherited runners to score in a four-run fifth inning during which the Giants (66-51) sent 10 batters to the plate.

“I knew coming in, these guys are sluggers,” Miley said.

Matt Wieters set a career high with five hits — three singles, a double and a triple — and scored both of the Orioles' first two runs. Adam Jones drove in Wieters on a two-out single in the fifth and on a seventh-inning sacrifice fly to right field.

The Orioles cut the Giants' lead to 7-5, and had two on with one out in the eighth, but reliever Derek Law induced a flyout to left from pinch hitter Steve Pearce, and Jones popped up to second to end the inning.

Manny Machado started the Orioles' ninth-inning rally with a one-out single, then moved to second base when the ball got by left fielder Angel Pagan. After Chris Davis struck out looking for the fourth time, Mark Trumbo drew a five-pitch walk to bring Schoop to the plate.

Schoop jumped on an 0-1 pitch from Casilla (2-4), sending a towering drive into the left-field seats that silenced the crowd.

Closer Zach Britton remained perfect by converting his 37th save attempt with a scoreless ninth inning after stranding the winning run on first base.

Wieters' record day: Wieters thought he had shown signs of breaking out of a deep slump in recent days, but he still didn't have many hits to show for it.

After his five hits Sunday, Wieters finally saw a payoff.

“Yeah, I'll take it,” Wieters said. “I finally had some balls to go through. It felt better the last couple of weeks, but today I actually was able to get the balls to go through.”

Trumbo hits homer No. 34: Mark Trumbo regained sole possession of the majors' home run lead by hitting a solo homer off Giants reliever Hunter Strickland in the eighth inning, his 34th of the season. It also matched his career high, set in 2011 with the Los Angeles Angels.

eencina@baltsun.com

twitter.com/EddieInTheYard