SAN FRANCISCO (AP) Giants manager Bruce Bochy didn't take kindly to what he believed were two intentional pitches thrown inside at Buster Posey, and wound up getting ejected for the first time this season.

Brandon Crawford felt the same way and paid the Diamondbacks back the best way he could.

Crawford drove in three runs and came within a triple of hitting for the cycle, Jeff Samardzija overcame a shaky first inning to win and San Francisco beat the Arizona Diamondbacks 6-2 on Friday night.

More from FoxSports

Crawford's home run came shortly after Diamondbacks starter Patrick Corbin (4-7) threw two consecutive pitches that nearly hit Posey. The second went behind Posey and nearly hit him in the back, causing home plate umpire Quinn Wolcott to warn both dugouts.

Bochy came out after Wolcott issued the warnings and was ejected.

''That kind of fired me up,'' Crawford said. ''When (Posey) walked I wanted to make them pay for doing that. I'm not going to sugarcoat it, that's what I was going for.''

Brandon Belt walked four times and scored for the Giants after earlier in the day being voted by fans to fill the final spot on the NL roster for next week's All-Star Game.

Crawford's homer was his ninth of the season, and he also singled, walked and doubled.

Corbin's pitches close to Posey followed on the heels of Samardzija hitting Arizona's Jean Segura - who homered leading off the game - in the top of the second.

''I understand they're not happy their guy got hit but I can't have Buster being a target,'' Bochy said. ''They threw once at him, throws another one that's up high at his back. That's enough. That's when guys get hurt.''

Diamondbacks manager Chip Hale, who was ejected in the fifth for arguing balls and strikes, defended Corbin.

''I just think he was trying to pitch in,'' Hale said. ''He was trying to pitch in and the ball got away a little bit.''

Samardzija (9-5) walked three and struck out six in 6 1/3 innings to win for the first time in four starts. The right-hander gave up two runs, both in the first inning. Samardzija also scored the go-ahead run on a throwing error after doubling leading off the fifth.

Three relievers completed the seven-hitter.

''It was a tough way to start the game but I like the way it finished,'' Samardzija said.

Crawford had an RBI single in the third, walked in the fifth and doubled in the seventh.

Corbin allowed five runs over 4 2/3 innings. The left-hander walked five with one strikeout.

Gregor Blanco hit a pinch-hit home run in the eighth for the Giants.

After the game the Diamondbacks announced they traded closer Brad Ziegler to the Boston Red Sox for two prospects.

HITTING PITCHERS

Corbin and Samardzija earn their money on the mound but both continue to show their skills at the plate. Corbin singled in the second inning to give him 13 hits this season, tops among all pitchers in the majors. Samardzija's double in the third is his third extra-base hit this season.

TRAINER'S ROOM

Diamondbacks: RHP Zack Grienke (left oblique strain) is feeling better but has not started playing catch. ... INF Chris Owings (left foot plantar fasciitis) and OF David Peralta (low back sprain) took batting practice before the game.

Giants: OF Hunter Pence (hamstring) ran the bases and will continue with baseball activities throughout the weekend. ... RHP Matt Cain allowed two runs over 5 2/3 innings with Triple-A Sacramento. . 2B Joe Panik (concussion) took grounders and hit off a tee before the game.

UP NEXT

Diamondbacks: LHP Robbie Ray (4-7) makes his third start against the Giants this season Saturday. Ray pitched six scoreless innings to win at AT&T Park on April 19.

Giants: RHP Jake Peavy (5-7) seeks his 18th career win against Arizona.