Jose Tabata's three-run double capped the big rally.
After St. Louis beat Atlanta earlier in the day, Pittsburgh needed a win to keep the division lead.
Garrett Jones and Jordy Mercer hit solo homers for the Pirates.
Jeanmar Lopez (3-0) got the win. Guillermo Moscoso (1-2) took the loss.
Marco Scutaro and Brandon Crawford each had two RBI for the Giants, losers of four of their last five games.
Susan Sarandon 'very excited' about daughter's pregnancy
Scarlett Johansson steps out with fiance after pregnancy reveal