SAN FRANCISCO (AP) As the seventh hitter due up in the ninth inning, Matt Duffy knew he wasn't likely to get another at-bat.
The Giants' young infielder calculated his chances anyway, down to the exact situation.
Duffy singled in Gregor Blanco with two outs in the ninth inning to cap a two-run rally off closer Steve Cishek and lift San Francisco to a 3-2 win over the Miami Marlins on Sunday.
''I did the math ... and I knew if I did come up it would be bases loaded with two outs, ninth inning, tie game,'' Duffy said. ''I was like `Let's hope we get it done before that, but just prepare for that.'''
The Giants had been quiet offensively most of the game until breaking out against Cishek (1-2), who had not allowed an earned run in 13 previous appearances against the defending World Series champs.
Cishek struck out Justin Maxwell to open the ninth before getting into trouble. Andrew Susac singled and Blanco doubled. Joaquin Arias pinch-ran for Susac and stayed at third when pinch-hitter Buster Posey was intentionally walked to load the bases.
After pinch-hitter Angel Pagan struck out, Cishek walked Nori Aoki to force in the tying run. Duffy, hitless in his previous four at-bats, followed with the first game-ending hit of his career.
Santiago Casilla (4-0) retired two batters for the win, helping the Giants split the four-game series. San Francisco went 7-3 on its 10-game homestand.
''That's sweet when it makes for a really nice homestand,'' San Francisco manager Bruce Bochy said. ''If we lose that one, it's so-so. Those guys in the ninth inning found a way. I thought it was huge win for us.''
Miami, which missed a chance to reach .500 for the first time this season, took a 2-1 lead when pinch-runner Donovan Solano scored on a wild pitch by Giants reliever Sergio Romo with two outs in the eighth.
Pinch-hitter Justin Bour singled off Romo with one out in the eighth. After hot-hitting Dee Gordon doubled Bour to third, Romo struck out Martin Prado. The next batter, Ichiro Suzuki, hit a foul ball that nearly landed fair moments before Romo's pitch skipped past Susac and rolled to the backstop.
Solano, who replaced Bour at third following Gordon's hit, immediately broke for the plate and slid home to beat Susac's throw to Romo.
It looked as if it would stand up as the game-winner until Cishek gave up two runs in the ninth.
''It felt like a good pitch (to Duffy) with the fastball down and away,'' Cishek said. ''But now I'm second-guessing myself there.''
The blown save by Miami's closer ruined a stellar start by Marlins right-hander Mat Latos.
Coming off his first win of the season against Washington on May 5, Latos retired 14 of the first 18 batters and allowed one run in seven innings.
Michael Morse, who spent last season with the Giants and received his World Series ring in a pre-game ceremony on Thursday, had been cheered every at-bat of the series until his ground-rule double off starter Ryan Vogelsong in the fourth drove in Christian Yelich to put Miami up 1-0.
San Francisco tied it on Brandon Belt's RBI single in the sixth.
Vogelsong had a second strong outing, but left with a no-decision. He gave up one run in seven innings with five strikeouts and no walks.
Marlins: Gordon's 54 hits through 29 games are tied for the third-most in history. Only Hank Aaron (60, 1960) and Johnny Frederick (55, 1930) had more.
Giants: Manager Bruce Bochy took advantage of the team's day off Monday and held Posey, Pagan and SS Brandon Crawford out of the starting lineup to give them extra rest. Of the three, only Pagan (finger) has a known injury. ... Crawford grounded out as a pinch-hitter in the seventh.
Marlins: RHP Tom Koehler (2-3) pitches against the Dodgers on Monday and has allowed seven home runs this season, all of them to left-handed hitters.
Giants: RHP Chris Heston (2-3) makes his first career start against the Houston Astros on Tuesday. It will also be the rookie pitcher's second interleague appearance.