SAN FRANCISCO — When the baseball rattled off the wall in the vast right-center-field expanse of AT&T Park — and out of the elastic grasp of Albert Almora Jr. — it set off a race for home between Brandon Crawford, who had been anchored at second base, and the inhabitants of the San Francisco Giants dugout, who sprinted to mob him.

As Crawford crossed the plate to end a 13-inning, five-hour thriller, the Giants staved off elimination with a 6-5 victory over the Chicago Cubs on Monday night that had more twists and turns than the city’s famed Lombard Street.

The winning hit, which narrowed the Cubs’ lead in this National League division series to 2-1, was delivered by Joe Panik, the second baseman whose last name seems to mock the idea that jangled nerves would ever get the best of the Giants.

It was a record 10th consecutive playoff victory for the Giants when they have faced elimination, and if there have been wins with greater rewards, as in Game 7 of the World Series two years ago, few have been as satisfying or hard-earned.