CHICAGO — It had the look of yet another stirring comeback for the major league-leading Chicago Cubs.

Trailing by a run, pinch hitter Anthony Rizzo led off the ninth inning with a walk and advanced to second on Dexter Fowler’s sacrifice. Only Rizzo rounded second too far and was tagged out for a double play that was the last in a series of uncharacteristic miscues.

Madison Bumgarner outpitched Jake Arrieta with 10 strikeouts over six innings, and San Francisco pounced on those mistakes for a 3-2 victory over Chicago on Saturday.

“It’s not very common with our group of guys,” Arrieta said. “You don’t expect to see those things often.”

Bumgarner (14-8) allowed two runs and five hits in his fourth victory in his last five starts. San Francisco, trying to catch Los Angeles in the NL West, rebounded from a pair of one-run losses in the first two games of the four-game set between potential postseason opponents.

“We’re going up against a team that, I don’t think they’ve clinched yet, but they’re going to be there,” Bumgarner said. “And we’re planning on being there. So it’s a playoff-type of matchup.”

The Giants entered the All-Star break a major league-best 57-33. But they are just 16-29 since that point, leaving them looking up at the Dodgers and clinging to the top spot in the wild-card race.

“It was one we definitely wanted and needed,” catcher Buster Posey said. “It was good to score some runs off Arrieta and then for our bullpen to hold the lead.”

Arrieta (16-6) was charged with three runs — two earned — and four hits in six innings in his first loss since July 30. Chicago had won five in a row overall.

“A little bad luck, a little missed execution,” Arrieta said.

After Rizzo was tagged out, Kris Bryant then hit a soft liner to shortstop Brandon Crawford, handing Santiago Casilla his 30th save.

“It was a great game,” Cubs manager Joe Maddon said. “You can nitpick it all you want, I love that game. That’s very similar to a playoff situation, things don’t always go right or according to plan.”

Chicago also had an opportunity in the sixth, but Bumgarner struck out Matt Szczur for the third time with two on. Hunter Strickland got Addison Russell to line to right with the bases loaded in the seventh.

The Cubs entered with a 16 1/2-game lead in the NL Central thanks to a number of tremendous defensive plays in the past week. But they were sloppy on a cloudless day with the wind blowing in as two of the game’s top pitchers squared off.

Third baseman Tommy La Stella’s two-out throwing error in the first made it 1-0. The Giants scored in the fourth with the help of a passed ball and added a run in the sixth on Arrieta’s second wild pitch.

The Cubs got within 2-1 in the fifth thanks to Javier Baez’s hustle. He led off with a single, stole second and scored from there when third baseman Eduardo Nunez was unable to barehand Arrieta’s slow roller.

Angel Pagan doubled and scored on the error by La Stella, who was a late addition after Jorge Soler was scratched with an ingrown toenail. Soler hit for La Stella in the fifth and struck out with one on.

The Giants took a 2-0 lead on Nunez’s RBI single in a fourth that included a walk and wild pitch by Arrieta and Willson Contreras’ passed ball.

TRAINER’S ROOM

Giants: Reliever Derek Law (elbow) hasn’t begun throwing yet, but manager Bruce Bochy said they “expect him to be at full strength” when he’s eligible to come off the disabled list Sept. 12.

Cubs: Setup man Hector Rondon (triceps) threw a 22-pitch simulated game and felt good enough that his rehab stint at Triple-A Iowa was scrapped. Rondon could be activated Monday. “I’m back to normal,” he said.

STREAKS OVER

The Cubs had won nine consecutive games at Wrigley Field and the Giants had lost six straight in Chicago.

WILD

Arrieta’s two wild pitches gave him an NL-high 12.

UP NEXT

Cubs RHP John Lackey (9-7, 3.41 ERA) has recovered from a shoulder injury and is expected to be activated off the disabled list to face Giants RHP Johnny Cueto (14-5, 2.98 ERA) on Sunday.