Gonzalez hit two-run homers in the third and seventh innings, the latter breaking a 2-2 tie and helping the Rockies to their sixth straight victory -- all on the road. Gonzalez has 12 homers, four in his past five games.
Gray (8-4) gave up two runs on seven hits and no walks in seven innings while striking out 10 for the second time this season. The first time came in a 6-3 victory at Chase Field on June 30, his first game back from the disabled list.
DJ LeMahieu singled off David Hernandez (2-1) with one out in the seventh inning before Gonzalez homered over the pool in right-center field, about the same spot as his first one landed.
Colorado (80-65) pulled with three games of Arizona (83-62) in the race for the first National League wild card. The Rockies remain 3 1/2 games ahead of the St. Louis Cardinals for the second wild card.
J.D. Martinez homered, his 22nd in 47 games since being acquired from the Detroit Tigers on July 18. David Peralta and Martinez each had two hits for Arizona.
Martinez doubled off Scott Oberg to open the ninth inning but was stranded as Chris Rusin got the final two outs for his second save.
Diamondbacks right-hander Taijuan Walker gave up two runs and four hits in six innings while dropping his ERA to 3.32. It has been lower only once this year. He struck out six and walked five, one intentionally.
Martinez led off the second inning with his 38th homer of the season, tying a career high set last year with Detroit, for a 1-0 lead.
Charlie Blackmon walked to open the third inning and scored on Gonzalez's one-out homer to make it 2-1.
Walker and Peralta singled to open the last of the third inning and Chris Iannetta was hit by a pitch to load the bases. Jake Lamb tied the game with a sacrifice fly before Paul Goldschmidt lined into a double play.
Rockies left fielder Gerardo Parra made running catches in front of the Arizona bullpen in the left field corner to take extra bases away from Goldschmidt in the first inning and Walker in the fourth. He appeared to chat with the Arizona relief corps after the first one.
Parra won a Gold Glove in left field for Arizona in 2011.
NOTES: Colorado LHP Kyle Freeland, who was forced to leave the Monday game after being struck in the left shoulder by a line drive in the fourth inning, played catch Tuesday and is expected to make his next start Saturday, manager Bud Black said. ... Colorado 3B Nolan Arenado became the first NL player with three straight 120-RBI seasons since Philadelphia 1B Ryan Howard when his four RBIs on Monday gave him 123. Howard had four straight (2006-09). ... Arizona RHP Randall Delgado had his original diagnosis of a flexor strain confirmed by Dr. James Andrews, the team said Tuesday, after films of his MRI were read. No treatment plan has been devised. Delgado already has been declared out for the season.