A double by Bumgarner helped set up Posey's slam in the fifth. Bumgarner homered the next inning -- he also hit a slam on April 11 against Colorado.
"All we could do was shake our heads," Posey said. "I was asking Rags (Dave Righetti) if any pitcher had ever hit two slams in the same season."
The last pitcher to launch two slams in a season was Tony Cloninger, who hit both in the same game for Atlanta on July 3, 1966, against the Giants at Candlestick Park.
Diamondbacks catcher Tuffy Gosewisch came out to talk to Matt Stites about how they were going to pitch to Bumgarner.
"Everybody knows he's a good hitter," Gosewisch said. "I wanted to go first pitch fastball down and away and it just ran back over the plate."
Bumgarner (10-7) threw 6-1/3 innings to end a personal three-game losing streak. He gave up four runs and 10 hits, striking out five.
Vidal Nuno (0-1) allowed four runs and six hits in five innings during his second start with Arizona.
The Giants finished the first half by winning five of nine. That followed a stretch of 18 losses in 23 games that cost them their lead in the NL West.
"The challenge for us is to look at the overall record," Posey said. "We're still in a good spot. We have to go forward."
Cody Ross hit a two-run homer for the Diamondbacks, who are 5-8 in their past 13 games. Martin Prado had three hits and drove in a run.
Nuno, acquired from the New York Yankees in a trade for Brandon McCarthy, had not allowed a run in his first 11 innings with the Diamondbacks before Posey's slam.
Bumgarner escaped a perilous sixth because of some fancy defensive plays, including his own.
Ender Inciarte and Prado each singled to start the inning. With Paul Goldschmidt batting, Bumgarner caught Inciarte taking off early for third for the first out.
Right fielder Hunter Pence, who also threw out a runner at third in the first inning, tracked down Goldschmidt's drive one step before slamming into the fence and third baseman Pablo Sandoval made a diving stop of Mark Trumbo's sharp grounder to end the inning.
Nuno breezed through the first four innings before running into trouble in the fifth. Bumgarner's one-out double started the rally. Pence was hit by a pitch and Sandoval, who had three hits, singled ahead of Posey, who homered into the left-field bleachers.
"I feel like we've been needing the big hit," Posey said. "I'm just trying to get a run on the board. Getting four across was great."
A double, an infield fielding error and a walk set up Bumgarner's slam in the sixth against Stites. Bumgarner earlier victimized Jorge De La Rosa of the Rockies.
"We had a good conversation about how we were going to attack Bumgarner," Stites said. "I just missed my spot. He's been swinging it well all year."
Goldschmidt went 0 for 4 and had his career best streak of safely reaching base end at 32 games.