SAN FRANCISCO — Giants manager Bruce Bochy thought he was playing the odds when he brought in closer Santiago Casilla to try to close out the ninth inning against St. Louis, even as a chorus of boos rained down from the sold-out crowd at AT&T Park.

For the third time in two weeks, the San Francisco skipper guessed wrong.

Kolten Wong hit a sacrifice fly off Casilla that capped a two-run comeback in the ninth and the Cardinals rallied for a big win in their playoff chase, beating the Giants 3-2 Saturday night.

“You go on history, that’s why the change was made,” Bochy said of his decision to replace Sergio Romo with Casilla after Jedd Gyorko’s one-out single in the ninth. “We were going with what we think is the best matchups. It didn’t play out tonight.”

Casilla (2-5) walked the first batter he faced, Yadier Molina, before Randal Grichuk hit a tying single.

Jose Martinez, pinch-running for Molina, took third on the hit and easily scored on Wong’s fly.

Casilla’s nine blown saves this season are tied with Nate Jones of the Chicago White Sox for most in the majors. It’s also the most blown saves by a Giants reliever since Robb Nenn had nine in 1999.

“I’m having bad luck,” Casilla said. “I was looking for a double play but we lose the game. I try to do my best. I’m not going to be sad or kill myself. It’s a game.”

Hunter Pence singled twice and Brandon Belt added an RBI double for the Giants, who fell five games behind the first-place Los Angeles Dodgers in the NL West.

The Cardinals had lost three straight and held a team meeting prior to the game. Their win further tightened the NL wild-card race — the Giants and Mets are tied for the top two spots, with the Cardinals trailing by two games.

Seung Hwan Oh (5-3) retired six batters for the win.

Brandon Moss hit his 27th homer for the Cardinals.

SHARK BIT

Giants starter Jeff Samardzija was lined up for the win until the Cardinals rallied. Samardzija allowed one run and five hits with four strikeouts and four walks over 6 2-3 innings, lowering his ERA to 2.74 over his past eight starts. “He got better as he went along,” Bochy said. “Gave us all we needed. We were a couple outs from winning.”

GOLD MEDAL MOMENT

Olympic gold medal-winning gymnast Aly Raisman, competing in nearby San Jose as part of a champions tour, threw out the ceremonial first pitch after spending time with several members of the Giants before the game. Raisman — who said she has only spent four days at home since returning from the Olympics — took pictures with Angel Pagan, and got tips from pitcher Johnny Cueto and hitting coach Hensley Meulens while sporting a Giants jersey. “I went to a lot of (Boston Red Sox) games with my dad growing up,” Raisman said. “I’m excited to be here. It was crazy when I came here and players know who I am.”

TRAINER’S ROOM

Cardinals: The team isn’t sure what the next step will be for OF Matt Holliday after he called off plans to bat in a simulated game earlier in the day because of swelling in his right thumb. Holliday has been out since Aug. 12 and had hoped the sim game would provide momentum for returning.

Giants: Bochy plans to give Buster Posey the day off Sunday in order to give the All-Star catcher a break before the team begins a seven-game road trip.

UP NEXT

Alex Reyes (1-2) starts for the Cardinals against Alberto Suarez (3-3) in the series finale.