Manny Machado hit a two-run homer off Max Scherzer with two outs in the seventh inning, then was hit by a Jonathan Papelbon pitch in the ninth causing benches to empty, and the Baltimore Orioles rallied past the Washington Nationals 4-3 on Wednesday night.

Steve Pearce had a two-run homer and a double for the Orioles, who moved within four games of the final AL wild card spot.

Washington remains 6 1-2 games behind NL-East leading New York, which lost to Atlanta.

More from FoxSports

J.J. Hardy led off the seventh with a double against Scherzer (12-12), but after Clint Robinson robbed Gerardo Parra with a diving stop at first, the Washington starter had a chance to get out of it.

Machado worked the count to 2-2 before lining Scherzer's season-high 122nd and final pitch into the seats in left center for his 30th homer.

Baltimore starter Chris Tillman (10-11) allowed three runs over six innings for his first win since Aug. 17.

Darren O'Day pitched the ninth for his fifth save.

Regular closer Zach Britton has not worked since Sunday, missing a second straight save opportunity.

Reliever Mychal Givens chipped in two scoreless innings for Baltimore.

Scherzer gave up four earned runs and seven hits while striking out 12 and walking two.

He has 249 strikeouts last on the season, and set a new Nationals (2005-present) record, surpassing Stephen Strasburg's 242 in 2014.

Scherzer has allowed 14 homers in his last nine starts.

Machado was nearly ejected in the fifth inning after home plate umpire Mark Ripperger declined to grant him time and Scherzer's pitch was a called strike three.

Manager Buck Showalter came out and steered Machado away from Ripperger.

The Nationals rallied for a 3-2 lead in the fifth.

Tillman had retired 12 straight before issuing a one-out walk to Michael Taylor. Wilson Ramos followed with a double down the left field line and Taylor came all the way around.

Ramos went to third on a single by Scherzer and scored on Anthony Rendon's sacrifice fly.

The Orioles took a 2-0 lead in the first. Chris Davis drew a two-out walk and Pearce followed with his 14th home run.

Washington pulled with 2-1 in their half on Yunel Escobar's RBI single.

TRAINER'S ROOM

Orioles: CF Adam Jones (back spasms) missed his second straight game. . RHP Miguel Gonzalez (right shoulder tendinitis) threw a three-inning simulated game as well as a 20-pitch bullpen session, and is expected to throw another bullpen session Saturday.

Nationals: 1B Ryan Zimmerman (oblique) has been taking light swings, but remains day-to-day. "When he goes out there and plays we want him to be able to let it go, so he's got to get to that point first," manager Matt Williams said.

UP NEXT

Baltimore RHP Tyler Wilson (2-2, 3.72) pitches the series finale, making his second start since being recalled from Triple-A Norfolk. Wilson allowed six runs in 4 1-3 innings of a loss to Tampa Bay last Friday. He will be opposed by RHP Tanner Roark (4-6, 4.73), who lost his last two starts, both against the Marlins.