DENVER — Cristhian Adames reacted too quickly on Joe Nathan’s first fastball and lined it foul. When he got the same pitch again he was right on it.

Adames hit a two-run double in the ninth inning off the veteran reliever to rally the Colorado Rockies past the San Francisco Giants 6-5 on Wednesday night.

Nolan Arenado homered to start the three-run rally, and Tom Murphy and David Dahl also went deep for the Rockies, who took two of three from the struggling Giants.

“It would have been disappointing losing the series but it’s always nice to win,” Arenado said. “When you walk off everybody’s feeling good.”

With Colorado trailing 5-3, Arenado led off the ninth with his NL-leading 37th home run off Santiago Casilla. After Murphy’s one-out single, Josh Osich (1-1) entered and hit pinch-hitter Charlie Blackmon. Nick Hundley’s bloop single off Nathan loaded the bases, and Adames lined a double off the wall in right to end it.

Adames was mobbed at second base by his teammates and doused with a bucket.

“I kind of pulled on a two-seamer just trying to get a groundball, trying to do anything I can to keep the ball on the ground,” Nathan said. “I left it in a bad spot. He put a swing on it and the rest is history.”

Jordan Lyles (4-4) pitched the ninth to get the win. It was a turnaround from last week when Colorado blew a six-run lead in the last two innings against the Dodgers and lost.

“We’ve had to swallow some tough losses,” Rockies manager Walt Weiss said. “You play close games, tough games, tight games — sometimes you get your heart broken. Sometimes you celebrate like we did tonight.”

The Giants hold the first NL wild-card spot by a half game over the St. Louis Cardinals and New York Mets. They are 17-32 since the All-Star break.

“You got to win games you’re supposed to win,” Giants manager Bruce Bochy said. “That’s two that has gotten away from us here.”

The Giants built an early lead on homers by Brandon Belt and Gorkys Hernandez, who had two hits each.

Hernandez, who turned 29 on Wednesday, doubled ahead of Belt’s two-run homer in the second and led off the fourth with his first home run since Oct. 2, 2012, with Miami, to make it 4-1.

The Rockies crept back into the game with consecutive home runs by Dahl and Murphy in the fourth. Hunter Pence made it a two-run game in the fifth with a double, one of his three hits.

The Giants couldn’t add on to their lead, or hold it in the ninth.

“Coming down the stretch we need every opportunity, every win we can get,” Nathan said. “When we get a chance to in the ninth we need to close those deals off and come away with a W.”

TRAINER’S ROOM

Rockies: 1B Gerardo Parra left the game following the first inning after suffering a right hamstring strain stretching for a throw from 3B Nolan Arenado. … Blackmon’s pinch-hit appearance in the ninth was his first action in five games because of lower back tightness. … RHP Jason Motte (right shoulder strain) threw 25-30 pitches in a simulated game. … 1B Mark Reynolds was away from the team for the birth of his son, Declan James, who arrived Tuesday. Reynolds will join the team in San Diego.

ARMS RACE

Bochy took advantage of the expanded rosters by stuffing his bullpen. San Francisco had 14 relievers available Wednesday, and the manager used eight, six in the final two innings.

“That’s why they’re good,” Arenado said. “They matchup and they know what they’re doing over there. Got to give him credit but tonight it worked out in our favor.”

UP NEXT

Rockies: RHP Jeff Hoffman (0-2, 6.60) goes for his first major league win in his fourth career start when the team opens a four-game series at San Diego on Thursday.