PHOENIX (AP) — Trevor Story hit a go-ahead single in the ninth after Dom Nunez tied it with a solo homer and the Colorado Rockies rallied from a seven-run deficit to beat the Arizona Diamondbacks 9-7 on Friday night.

Colorado trailed 7-0 after three innings and 7-6 in the ninth, but Nunez led off with a drive that just cleared the right-field wall and tied it. With one out, Brendan Rodgers doubled, Charlie Blackmon walked and Story hit a soft liner that fell into right-center field and scored Rodgers.

C.J. Cron added an RBI groundout to give the Rockies a 9-7 lead. The Rockies — who have a 75-85 record this year — have an 11-8 mark over the last few weeks.

“It's a high-energy clubhouse right now,” Nunez said. “Late in the year, it shows the type of team we have. We have a no-quit mentality and we're going to fight to the end. Hopefully we can take this momentum into next year.”

Lucas Gilbreath (3-2) worked out of a jam in the eighth to earn the win. Carlos Estevez pitched the ninth for his 11th save.

D-backs pitcher Humberto Castellanos sparked a seven-run third inning with a surprise three-run blast down the left-field line, turning on the first pitch from Jon Gray and yanking it into Arizona's bullpen. Ketel Marte belted a solo homer and Christian Walker added a two-run shot to push the Diamondbacks to a 7-0 lead.

It was Castellanos' first big league homer and drew big cheers from the Diamondbacks dugout, providing a light-hearted moment in a tough season. But Arizona eventually found another way to lose in a season full of setbacks, dropping its 110th game this year.

The D-backs must win at least one of the season's final two games to avoid having the worst season in franchise history. They had a 51-111 mark in 2004.

Castellanos crusied through five innings before the Rockies rallied for six runs in the sixth. Sam Hilliard had the big blow with an opposite-field three-run homer to cut the D-backs' lead to 7-6.

Said Nunez: “We just woke up and kind of erupted.”

“There was a tangible feeling that what we did tonight could happen,” Colorado manager Bud Black said. “That’s what you want from your team.”

Castellanos gave up six runs over 5 2/3 innings, giving up nine hits, one walk and striking out four. Caleb Smith gave up the tying homer and J.B. Wendelken (3-3) took the loss after allowing two runs. Even for the D-backs, it was a stunning collapse.

“I’m not doing too good right now — not going to lie about that,” D-backs manager Torey Lovullo said. “It’s the hardest part of sports when you do a lot right and you still don’t find a way to win a game.”

Gray (8-13) had a rough final outing to his season. The right-hander looked untouchable in the first two innings, striking out four of his first six batters. The outing fell apart in the third, and he lasted just four innings, giving up seven runs on seven hits. He struck out seven.

STORY'S SEASON

Story stole his 20th base of the season, swiping second base in the seventh.

Story — who also has 24 homers — reached the 20-20 mark for the third time in his career. He's just the seventh player to have 150 homers and 100 stolen bases in his first six seasons, joining Willie Mays, Bobby Bonds, Darryl Strawberry, Andruw Jones, Ryan Braun and Mike Trout.

MARVELOUS MARTE

It's been lost in a mostly dismal season for the Diamondbacks, but Marte has been one of the National League's best all-around hitters when healthy.

Marte's solo shot in the third was his 14th of the season. He came into the game batting .315 and has 27 doubles despite playing in just 88 games this season.

The 27-year-old figures to be a big part of the D-backs' rebuilding effort over the next few seasons. He's signed through 2022 and has a team-friendly option for the 2023 and 2024 seasons. Marte — who has battled hamstring injuries this season — has played mostly center field this season but also plays second base.

UP NEXT

The Diamondbacks send RHP Zac Gallen (3-10, 4.37 ERA) to the mound for his final start of the season on Saturday. He'll face Rockies RHP Antonio Senzatela (4-9, 4.10).

___

More AP MLB: https://apnews.com/hub/MLB and https://twitter.com/AP_Sports