Updated

Melky Cabrera smoked a two-run home run as part of a five-run fourth inning, and the San Francisco Giants held on for a 6-5 victory over the Pittsburgh Pirates in the opener of a three-game set at PNC Park.

Ryan Theriot ripped an RBI double and knocked in a run with a sacrifice fly, and Hector Sanchez added an RBI base hit in the victory for the Giants, who got back in the win column after being swept by the Washington Nationals earlier in the week.

Barry Zito (7-6) allowed four runs on seven hits and three walks with three strikeouts over five innings to earn the win. Sergio Romo collected his fifth save of the season with a scoreless ninth inning.

"After a tough series in Washington, that's a great way to bounce back," Giants manager Bruce Bochy said. "It was a great performance by the bullpen."

Andrew McCutchen went 3-for-5 with an RBI triple, while Casey McGehee and Pedro Alvarez clubbed solo home runs for the Bucs, who had a four-game winning streak halted.

Erik Bedard (4-10) yielded five runs -- four earned -- on four hits and three walks over just 3 2/3 innings to take the loss. He struck out one hitter.

It's only the fourth loss for the Pirates in their last 20 home games.

After registering no hits and trailing 3-0 through three innings, the Giants finally got their offense in gear in the fourth.

Justin Christian started the five-run frame by working a walk before scoring on a double by Theriot. Cabrera followed by ripping a breaking ball over the wall in left field to tie the game.

Buster Posey worked a walk, moved to second on an infield single, third on a flyout and scored on Sanchez's single to left, putting the Giants ahead. Joaquin Arias gave San Francisco a two-run lead when he laced a line drive off Josh Harrison at shortstop and into left, which allowed Pablo Sandoval to score.

"Even though we had that one bad inning, I thought we played pretty good baseball," Alvarez said. "We don't stop until the final out is made."

McGehee brought the Pirates within one in the fifth when he laced a fastball off Zito into the seats in left-center, but the Giants got it right back in the seventh courtesy of a sacrifice fly by Theriot to extend the lead to 6-4.

Facing Jose Lopez in the eighth, Alvarez ripped a hanging curveball into the seats in right-center to again bring Pittsburgh within one, but Romo retired the side in order in the ninth to preserve the win.

Earlier, the Pirates opened the scoring with a run in the first inning. Harrison and Drew Sutton began the frame with back-to-back singles, and McCutchen knocked home Harrison with a base hit into left field for the first run of the game.

Pittsburgh tacked on two more in the third. McCutchen ripped a triple into the left-center field gap to plate Harrison, who worked a leadoff walk. Neil Walker doubled home McCutchen two batters later to extend the lead to 3-0.

Game Notes

The Pirates have hit at least one extra-base hit in a season-high 37 consecutive games...San Francisco has won 14 of their last 20 games against the Pirates and the first game of a series in 11 of their last 14 sets...San Francisco snapped a three-game road skid and improved to 20-22 away from AT&T Park this season...The Giants plated five runs in an inning for the second time this year.