PHOENIX — J.D. Martinez lined a two-out RBI single to deep left field with the bases loaded in the ninth inning, and the Arizona Diamondbacks beat the Miami Marlins 3-2 on Sunday to clinch the top wild card in the National League.

The Diamondbacks, who won only 69 games a year ago but are 90-66 this season, were assured a playoff berth in the fourth inning after St. Louis and Milwaukee lost. The comeback victory ensured Arizona will host the NL wild-card game Oct. 4.

Fernando Rodney (5-4) pitched a perfect inning for the win.

A throwing error by Justin Nicolino (2-3) on Kristopher Negron’s sacrifice bunt helped load the bases with no outs in the ninth.

ROCKIES 8, PADRES 4

SAN DIEGO — Gerardo Parra hit a tiebreaking single in a two-run third inning, Pat Valaika and Charlie Blackmon hit consecutive home runs in the ninth and Colorado opened a two-game lead over Milwaukee for the second NL wild card. At 41-40, the Rockies tied their record for road wins, set in 2009.

German Marquez (11-7) allowed two runs, five hits and three walks in five innings.

Luis Perdomo (8-11) gave up four runs and six hits in five innings.

CUBS 5, BREWERS 0

MILWAUKEE — Jose Quintana (7-3) pitched a three-hitter and struck out 10 in his second big league shutout as Chicago opened a 5½-game lead over second-place Milwaukee in the NL Central.

Kris Bryant hit a go-ahead double in the fourth off Chase Anderson (11-4), Ben Zobrist had a two-run homer in the seventh and Anthony Rizzo added a two-run double in the eighth against Carlos Torres.

PIRATES 4, CARDINALS 1

PITTSBURGH — Seung Hwan Oh (1-6) allowed a go-ahead, two-run homer in the fifth to Starling Marte and rookie Jordan Luplow also went deep as St. Louis fell 2½ games behind the Rockies.

Jameson Taillon (8-7) allowed one run and four hits in five innings. Five pitchers combined for hitless relief, with Felipe Rivero striking out two in a perfect ninth for his 19th save.

BLUE JAYS 9, YANKEES 5

TORONTO — Jose Bautista had two hits and a walk in what was likely his final home game with the Blue Jays, leaving to a standing ovation during the ninth inning. Bautista, who turns 37 next month, is hitting .203 with 22 homers and 62 RBIs, and Toronto appears unlikely to exercise its half of a $17 million mutual option.

Aaron Judge homered twice for the Yankees, increasing his AL-leading total to 48 — one shy of the major league rookie record set by Mark McGwire in 1987. Judge has 11 home runs in September.

A day after clinching a postseason berth, New York dropped five games behind AL East-leading Boston with seven games to play.

Jaime Garcia (1-3) jeopardized his chance of making New York’s postseason roster, allowing five runs, four hits and three walks in 2 1/3 innings.

RED SOX 5, REDS 4

CINCINNATI — Mookie Betts hit a tying three-run double in the eighth off Raisel Iglesias (3-3) and dashed home from second base on Rafael Devers’ infield single as Boston won for the 14th time in 17 games.

Robby Scott (2-1) won, and Craig Kimbrel pitched the ninth for his 35th save and hasn’t allowed a run in his last 11 appearances.

Billy Hamilton helped the Reds go up 4-1. He tripled home a run and brought the crowd to its feet by scoring after getting caught in a rundown between first and second. Second baseman Dustin Pedroia made a wild throw, and Hamilton kept going when nobody covered home.

DODGERS 3, GIANTS 1

LOS ANGELES — Clayton Kershaw (18-4) became the NL’s first 18-game winner, pitching shutout ball until Mac Williamson’s home run in the eighth, his final inning.

Kershaw lowered his league-best ERA to 2.21 and struck out six to become the first Dodgers pitcher to reach 200 seven times.

Brandon Morrow worked a perfect ninth for his second save.

Yasmani Grandal hit a sacrifice fly in the first against Chris Stratton (3-4) and a homer in the fourth.

INDIANS 4, MARINERS 2

SEATTLE — Corey Kluber (18-4) won his sixth straight start and struck out 10 in becoming the AL’s first 18-game winner, allowing Ben Gamel’s tying two-run homer in the fifth, which stopped his scoreless streak at 26 2/3 innings.

Jose Ramirez’s 29th home run put Cleveland ahead in the sixth against Mike Leake (3-1). Cody Allen worked the ninth for his 29th save as Cleveland won for the 29th time in 31 games and eliminated Seattle from postseason contention.

TWINS 10, TIGERS 4

DETROIT — Jorge Polanco hit a solo homer in the first off Buck Farmer (4-5), Eduardo Escobar welcomed Victor Alcantara with a three-run drive in the sixth and Minnesota completed a four-game sweep, outscoring Detroit 39-12.

Jose Berrios (13-8) won for the first time since he beat the Chicago White Sox on Aug. 30, allowing two runs and five hits in six innings.

Minnesota has a 4 1/2-game lead over the Los Angeles Angels and a 5½-game advantage over Kansas City and Texas for the second AL wild card. The Twins outscored the Tigers 39-12 in the sweep.

Detroit has lost seven straight and 14 of 16.

ANGELS 7, ASTROS 5

HOUSTON — Luis Valbuena hit a tiebreaking two-run double in the seventh and Justin Upton tacked on an insurance run with a solo homer in the eighth, helping Los Angeles Angels snap a six-game skid.

The Angels are 4 1/2 games behind Minnesota for the second AL wild card.

Valbuena hit his bases-loaded, go-ahead double with no outs off Chris Devenski (8-5). Keynan Middleton (6-1) pitched a scoreless sixth for the win and Blake Parker struck out one in a perfect ninth for his seventh save.

WHITE SOX 8, ROYALS 1

CHICAGO — Lucas Giolito (3-3) allowed one run in seven innings, Avisail Garcia drove in three runs with his 18th homer and Chicago dropped Kansas City

5½ games behind the Twins.

Ian Kennedy (4-13) allowed five runs in 5 1/3 innings as he lost his seventh straight decision.

ATHLETICS 8, RANGERS 1

OAKLAND, Calif. — Khris Davis hit his 41st home run, a drive off Martin Perez (12-12) in a five-run fifth inning, as Oakland extended its winning streak to a season-high seven.

Jharel Cotton (9-1) pitched five shutout innings of one-hit ball to win his fourth straight decision, dropping the Rangers 5 1/2 games behind Minnesota.

ORIOLES 9, RAYS 4

BALTIMORE — J.J. Hardy hitting a crowd-pleasing, go-ahead two-run homer off Chris Archer (9-12) in what was likely his last home game with Baltimore. Rookie Chance Sisco also homered for the Orioles, who earned a split of the four-game series to finish 46-35 at home.

Mychal Givens (8-1) got five outs for the win, dropped the Rays six games behind Minnesota with six to play.

NATIONALS 3, METS 2

NEW YORK — Max Scherzer (16-6) struck out 10 and allowed three hits over six innings as Washington clinched home-field advantage in the NL Division Series.

Trea Turner hit a go-ahead, two-run homer off Jacob deGrom (15-10), and Jose Lobaton had an RBI single. Brandon Kintzler worked the ninth for his 29th save, retiring Nori Aoki on a game-ending groundout with runners at second and third.

PHILLIES 2, BRAVES 0

ATLANTA — Nick Pivetta (7-10) allowed five hits in six innings to outpitch Luiz Gohara (1-3) in a matchup of rookies, and Mikael Franco hit a tiebreaking home run in the fifth as Atlanta finished 37-44 in its first season at SunTrust Park.

Aaron Altherr added an RBI double in the eighth off A.J. Minter. Hector Neris pitched out of trouble for his 24th save.