The Marlins snapped a two-game skid and for at least one night avoided becoming the first team in the majors to lose 50 games this season.
Miami managed only two hits over the first seven innings, but produced the deciding runs in the eighth against Jeremy Affeldt (1-3).
Affeldt came on to replace Sandy Rosario after Rosario walked Justin Ruggiano to open the eighth. Affeldt promptly hit Derek Dietrich with a pitch and both runners moved up on a sacrifice by Adeiny Hechavarria.
Jeff Mathis then struck out to give the Giants a chance to get out of the inning, but Ozuna came on as a pinch hitter and singled to right to bring home the tying and go-ahead runs.
Chad Walls worked a perfect eighth to protect the lead and Steve Cishek survived a one-out single in the ninth to pick up his 11th save.
Starter Tom Koehler survived seven innings and won his first game of the year when Miami staged its comeback. Koehler (1-5) allowed a run on three hits with three walks and three strikeouts.