Martinez (2-3) also drove in a run for the Cardinals, who won their second straight after losing three in a row. Martinez allowed a run and five hits in five innings. He walked one and struck out six.
Madison Bumgarner (9-6) fell to 0-3 over his past four home starts after allowing five runs — four earned — and six hits over five innings. He walked three and struck out six. Bumgarner also drove in a run for the Giants, who have lost 17 of 22 and fell out of first place in the NL West.
Matt Carpenter continued ripping Giants pitching, extending his hitting streak against them to 11 games with a 2-for-4 effort. He has a .519 (28 for 54) career average against the Giants and has hit safely in 18 of his 20 games, including the postseason, against them. Yadier Molina added three hits and Oscar Tavares, John Jay and Shane Robinson each drove in a run for the Cardinals.
Peralta's first-inning home run put St. Louis ahead to stay. Bumgarner drove in a run for the Giants in the third, but then gave up three runs in the fourth — two on a single from Martinez.
Robinson, pinch hitting for Martinez in the sixth, tripled home a run. Tavares added an RBI in the seventh.
The Giants loaded the bases with two outs in the fifth, but Martinez got Buster Posey on a swinging strike three to end the threat.
In the sixth, Tyler Colvin walked, Adam Duvall singled and Brandon Crawford punched a single into right field. An error allowed Colvin to score.
NOTES: Giants INF Joe Panik batted second, where he hit for Triple-A Fresno, as manager Bruce Bochy tweaked the lineup. "He has some sense of comfort there," Bochy said. . OF Hunter Pence hit lead off and OF Gregor Blanco was dropped to eighth. . INF Marco Scutaro continues to make progress and if he's fine after playing for the second straight day for the Arizona Rookie League Giants, he could be elevated to Fresno by Sunday. . RHP Matt Cain (1-6, 4.38) pitches for the Giants when they open a three-game series at San Diego on Friday. He's 7-13 lifetime against the Padres. . RHP Lance Lynn (8-6, 3.38) pitches for the Cardinals when they open a three-game set with the Miami Marlins in St. Louis. He's 2-0 with a 5.79 ERA against the Marlins. . Cardinals OF Allen Craig was given a day off. He was hitting .176 on the road trip. . Cardinals RHP Joe Kelly will make two more rehab starts and return after the All-Star Break.
Link to box score: St. Louis (46-40) 7 at San Francisco (47-38) 2
Join the pre-game, in-game and post-game discussion about each contest at The Cardinal Nation message board.