Gregor Blanco, who drove in the Giants' only other run with a fifth-inning triple, notched an infield single against Mike Dunn (2-2) leading off the 11th.
Ryan Webb came in to pitch and Marco Scutaro put down a sacrifice bunt, sending Blanco (3-for-5) to second. Blanco advanced to third on Buster Posey's infield hit, then scored the walk-off game-winner when pinch-hitter Sanchez blooped a hit to left.
The hit made a winner of Sandy Rosario (2-0), who pitched 1 2/3 scoreless innings as the Giants rebounded from losses to Miami in each of the first two contests of the four-game series.
San Francisco starter Barry Zito limited the Marlins to one run on six hits over seven innings.
Miami's Jacob Turner also pitched well, like Zito yielding just one run on six hits in seven frames.
Ed Lucas went 3-for-4 with his first major league homer for the Marlins.
Couple calls 9-1-1 over missing hash browns; assault McDonanld's employees
Exploding whale video goes viral on Internet