Mike Yastrzemski hit a solo homer for the Giants, who have won four straight games
San Francisco starter Alex Cobb gave up one run and four hits in six innings, with five strikeouts and two walks.
Taylor Rogers (5-3) pitched a scoreless seventh.
Camilo Doval pitched the ninth for his 28th save, tops in baseball.
Henry Davis hit a solo homer for the Pirates, who are 2-9 in their past 11 games
Pittsburgh starter Johan Oviedo, who had lost seven decisions in a row, allowed one hit, Yastrzemski's homer, over seven innings. He matched a career high with 10 strikeouts and walked three.
Carmen Mlodzinski (1-2) recorded one out and was charged with both eighth-inning runs. Only one was earned.
With one out in the second, Yastrzemski hit his 11th homer, to right-center, for a 1-0 San Francisco lead.
Pittsburgh came right back. On the first pitch of the bottom of the second, Davis hit his second homer, to left, for a 1-1 tie.
The Giants had a scare in the fifth. Cobb, an All-Star, took a comebacker by Bryan Reynolds on what ended up being the third out. The ball appeared to hit Cobb flush near his left ankle. He went down, bounced up quickly and walked off on his own. He received medical attention in the dugout and was able pitch one more inning
In the eighth against Mlodzinski, LaMonte Wade Jr. drew a four-pitch walk and went to second on Joc Pederson's single to right. David Bednar relieved Mlodzinski and struck out J.D. Davis.
Conforto singled to right. Wade scored and, when rookie second baseman Nick Gonzales took a cutoff throw but did not immediately throw home, Pederson also scored to make it 3-1.
There was a weather delay of about 40 minutes at the start of the game.
--Field Level Media