With the win, the D-backs moved to within four games of the National League's second Wild Card berth. The Giants, who are in first place in the NL West, come to town Friday for a three-game series. The D-backs took two of three against the Giants last week in San Francisco.
"It's getting better," D-backs manager Kirk Gibson said of the team's standing in the playoff race. "We need to keep running in the same direction. Things are getting interesting. We've got a very tough team coming in we know. We played well against them the last time we were at their place and it's going to have to be the same type of deal. There's a lot of teams involved. It's good, it's exciting and the energy should be good."
It was the second straight solid outing for Cahill, who beat the Giants in his previous start. He allowed four hits and did not issue a walk while striking out seven to raise his record to 11-11.
Gonzalez's two-run double did the damage for the Dodgers in the first inning after Cahill allowed a single to Shane Victorino and hit Matt Kemp with a pitch.
"He kept it together," Gibson said of Cahill.
Cahill said he felt like he was trying to do too much early on but was able to make the adjustment.
"I was just throwing strikes and just trying to go right after them," Cahill said. "They're a good team, so if you fall behind they're going to hurt you, so after that I was just trying to get ahead."
The D-backs began to chip away at the lead in the second when Paul Goldschmidt led off with a single against Aaron Harang. Goldschmidt, who stole second and moved to third on a wild pitch, eventually scored on Justin Upton's sacrifice fly to right.
"Goldy is sneaky fast and you hit balls to the outfield he's going to score for you," Upton said.
Things fell apart for Harang after he retired the first two hitters in the sixth.
Goldschmidt and Miguel Montero followed to draw back-to-back walks and Upton's ground-ball single to left scored Goldschmidt and moved Montero to third.
"I was just trying to keep the line moving and hand the baton over to [Gerardo] Parra," Upton said.
Parra then squirted a single to left off left-handed specialist Randy Choate to score Montero and give the D-backs a 3-2 lead.
David Hernandez closed things out in the ninth for the second straight night to pick up his fourth save of the year. Hernandez was pitching in place of closer J.J. Putz, who has been hampered by a stiff back.
It was certainly not a comfortable save given that Hernandez had to face the heart of the Dodgers order in Kemp, Gonzalez and Hanley Ramirez.
"As a closer, that's who you want to face," Hernandez said. "You want to be able to go in and know that you can come in and get those guys out. They're not always going to be three-run saves so you take them as you get them."
It was first time since the season-opening series against the Giants that the D-backs managed back-to-back one-run wins.
"To win back-to-back one-run ballgames under pressure in big situations, that can build momentum," Upton said. "Hopefully we build off it and continue to win ballgames."