SAN FRANCISCO — Driven by their hunger, the seagulls congregated above McCovey Cove on Monday night awaiting their next meal. Even with their tiny little bird brains, it’s as if they knew the Giants finally might be through.

For 13 excruciating innings, with their season on the line, the Giants traded haymakers with the Cubs, punches that would have long since flattened lesser foes.

But over and over, bone-rattling blows were not enough to decide Game 3 of this National League Division Series.

Only when Joe Panik ripped his game-winning double off the wall in right-center could the Giants claim a riveting 6-5 victory that prolonged their season and denied the Cubs a three-game sweep.

“I really don’t doubt,” Panik said shortly after knocking in Brandon Crawford to extend the Giants’ winning streak in elimination games to 10, the longest in postseason history.

The Giants prevailed on a night in which Madison Bumgarner looked human. “That’s one of the best, most exciting games I’ve ever been involved in,” manager Bruce Bochy said. “They found a way.”

advertisement | advertise on newsday

When the Giants’ Conor Gillaspie laced a two-run triple and scored on Crawford’s single to give his team its first lead in the eighth, it was answered with Kris Bryant’s tying two-run homer in the ninth.

After Cubs rightfielder Albert Almora Jr. laid out to catch Buster Posey’s potential game-winning drive in the ninth and start a double play to send the game into extra innings, Giants centerfielder Denard Span laid out to deprive Almora of a rally-starting hit in the 12th.

But the Giants finally emerged as victors in the 13th, forcing a Game 4 Tuesday night. It came at 2:43 a.m. EDT, ending a 5- hour, 4-minute testament to why the Giants have become masters of October.

“Every team believes in themselves, I feel,” Giants reliever Sergio Romo said. “But there are a few that have proof that the belief is real, that belief is there.”

Crawford doubled to begin the 13th off weary lefty Mike Montgomery, beginning his fifth inning of relief, a season high. Panik made the rally count, raising his right fist as the ball struck the fence and bounded away.

Baseball videos

Crawford scored and his relieved teammates congregated at third base to mob Panik.

“Just because we’re down doesn’t mean we’re out,” Panik said. “If we’re breathing, we’re still fighting.”

With that came the end of a remarkable test of endurance, one that forced the Giants to exert every bit of their championship pedigree and forced the Cubs to reach down and demonstrate the will that guided them to 103 victories this season.

The Giants — World Series champions in 2010, 2012 and 2014 — proved they would not bow out quietly in a sweep.

From the start, Game 3 was destined to become a classic.

advertisement | advertise on newsday

The Cubs got to Bumgarner, with Cubs righthander Jake Arrieta hitting a three-run homer on a 1-and-2 pitch with two outs in the second inning that changed the tenor of the night.

With two on and nobody out in the eighth, the Cubs handed the ball to Aroldis Chapman, tasked with preserving a 3-2 lead by getting the final six outs.

But Gillaspie found more magic. After ending the Mets’ season with a three-run homer off closer Jeurys Familia in the ninth inning of Wednesday night’s wild-card game, Gillaspie laced a 102-mph fastball to the expansive gap in right-center for a two-run triple that Almora dived for but could not stab.

Crawford followed with an RBI single, giving the Giants a 5-3 lead for a shaky bullpen that was at the center of an ugly second-half collapse. The Giants then put runners on second and third with one out but stranded them, and it proved costly.

In the ninth, Romo walked Dexter Fowler and watched in horror as Bryant, perhaps the National League’s Most Valuable Player, crushed a tying two-run shot just over the fence in leftfield to tie the score at 5-5.

advertisement | advertise on newsday

“I didn’t think at the time that he hit it as well as he did,” Romo said.

The Giants rose up again in the ninth as Brandon Belt walked and Posey laced a drive to rightfield. But Almora, a defensive whiz who entered the game for his glove, made a fully extended dive to deprive Posey of what would have been the game-winning hit. Then he threw to first to double off Belt.

It was another heart-stopping play in a night that began with the unraveling of a postseason legend.

Entering his start, Bumgarner was 8-3 with a 1.94 ERA in the postseason. But he allowed seven hits in five innings, including Arrieta’s stunning three-run homer.

In his last outing, a four-hit shutout of the Mets in the wild- card game, Bumgarner pelted the upper portion of the strike zone with pinpoint command of his fastballs. They were meant to be hit weakly.

The Mets obliged. Entering the game, they had made it a point to swing early if the opportunity arose, well aware of the fact that their best chance to do damage might come early in the count. But they missed their chances. Bumgarner needed only 21 pitches to navigate the first three innings. He got no such reprieve from the Cubs, who got a boost from Arrieta, one of baseball’s best-hitting pitchers.

Bumgarner grooved a belt-high 92-mph fastball over the plate and Arrieta put a charge into it. Perhaps the only consolation is that home run had come in the second, plenty of time for the Giants to recover.

“It was so early in the game that we never felt out of it,” Belt said.

The Giants were faced with overcoming a 3-0 deficit to extend their season and the Cubs were forced to show they could put away baseball’s most battle-tested team.

It would take 13 tension-filled innings to learn the answer. In the end, the Giants shuffled away with new life, and the sea gulls swooped in for their long-awaited meal.

Said Posey: “These are the games that you remember when you’re done playing.”