Asdrubal Cabrera knew right away what he had done. Before disbelief sent the crowd into a frenzy, before relief sent his teammates swarming onto the field, he knew.

So with both hands, Cabrera heaved his bat as he jogged up the first base line, flipping it as high and as far as he could. He disposed of it the same way that the Mets had shed their pain in a victory that exemplified what has been an improbable season.

“As soon as I hit it, I knew the ball was going to be out,” Cabrera said Thursday night after his three-run shot in the 11th inning lifted the Mets past the Phillies, 9-8. “I was just so excited.”

In baseball, cruelty prefers a grand entrance, often at the absolute worst time. With just nine games left in the season, the Mets know this all too well. In the heat of a three-way battle for two wild-card spots, they had come to the brink of losing four straight games, until Cabrera turned a crushing loss into a signature victory.

It took a franchise-record 27 players and Jose Reyes’ two-run blast in the ninth to force extras. It took the tenacity to weather six lead changes and yet another near miss, when Lucas Duda’s long drive in the 10th missed the foul pole by inches.

It took a bit of luck, when hitting coaches Kevin Long and Pat Roessler talked manager Terry Collins out of hitting Cabrera fourth, which would have taken that bat out of his hands in the pivotal moment of the game.

advertisement | advertise on newsday

But in the end, the Mets emerged with a victory that Collins said captured “exactly what the season is like, up and down, up and down.”

Until Thursday night, the Mets (81-72) had gone 0-for-63 this season when trailing after eight innings. But because they came back, the Mets took a half-game lead over the idle Cardinals and remained tied with the Giants in the wild-card race.

“I can’t even explain it, man,” Reyes said. “I’m still excited about that game. It was great to see coming from behind like that. That’s what a good team does.”

Yoenis Cespedes delivered a pair of clutch hits, driving in the tying run in the fifth and the go-ahead run in the seventh to give the Mets a 4-3 lead. But Phillies third baseman Maikel Franco hammered a three-run shot off Addison Reed in the eighth, sending a wave of dread through Citi Field.

Reyes’ game-tying homer gave the Mets new life. He sprinted around the bases and danced in the dugout.

Mets videos

“An unbelievable moment,” said Reyes, who admitted later that he couldn’t remember the last time he had been so emotional for a homer.

In the 10th, Duda came off the bench and nearly gave the Mets a storybook ending. Recently returned after missing much of the season with an injured back, Duda cracked what looked to be a game-winning homer down the rightfield line. But the ball tailed away mere inches to the right of the foul pole.

Duda ultimately struck out. It came one night after Braves outfielder Ender Inciarte’s leaping grab at the fence robbed Cespedes of a walk-off blast.

In consecutive nights, the Mets missed out on game-winners by a combined distance of about a foot. Again, things looked bleak for the Mets.

A.J. Ellis, the light-hitting veteran catcher, blooped a run-scoring single to rightfield off Mets closer Jeurys Familia in the 11th. Later, Jim Henderson walked Franco to force in an insurance run.

advertisement | advertise on newsday

But Cabrera would not be denied. He has willed his way into the lineup, playing through a knee injury that has hobbled him since spring training. He would not miss his chance. His 22nd homer of the year sailed over the rightfield fence, far from the reach of calamity.

Overcome with relief, Cabrera flipped his bat, raised his arms, then rounded the bases. At home plate, he let himself get absorbed into the waiting arms of his teammates.

“This game is so crazy, you know?” Cabrera said after the fourth walk-off homer of his career. “But like I’ve been saying, we fought, we battle every inning, we never put [our] heads down.”