Stanford escaped the wilds of Oregon on Saturday night with a 38-31 overtime victory that was virtually handed to them, like an absurdly generous gift. They won’t worry about the details. In the end, this was about reputation and identity, each fortified beyond measure for coach David Shaw’s program.

The reputation is of toughness, and for a team significantly outplayed for much of the evening, Stanford scored one of the most steel-minded wins of Shaw’s tenure. Perhaps that isn’t so surprising. What’s caught the attention of the rest of the Pac-12, in incremental doses as the season goes on, is the Cardinal’s ability to crush people with their passing game.

So much of Shaw’s offense still revolves around running back Bryce Love, and he certainly had his moments in the game that kept Stanford unbeaten. But this was a comeback from the depths, achievable only through the air, and on a night when Oregon’s Justin Herbert so impressively resembled an NFL quarterback, it was the Cardinal’s K.J. Costello who stole victory from Herbert, the Ducks, and an Autzen Stadium crowd that could barely comprehend what took place.

First, consider the gift — two of them, actually. Late in the third quarter, an apparent touchdown run by Oregon’s Jaylon Redd was ruled to have been inches short of the goal line. No matter: The Ducks seemed certain to finish the job for a 31-7 lead, a lead that likely would hold up with so much momentum in their favor. (How bad was it for Stanford? At one point, Herbert had out-rushed Love, 42-41.)

But wait: With Herbert in shotgun formation, a ridiculously high snap by center Jake Hanson sailed over the quarterback’s head, and once linebacker Joey Alfieri scooped it up, he was gone — 80 yards for a touchdown and a 24-14 score. If you’re talking about momentum, this was about as radical a shift as the sport of football can produce.

It was now up to Costello to maintain his team’s revived spirit, and as the comeback progressed, some marvelous things took place.

For one, wide receiver JJ Arcega-Whiteside continued to show why he’s among the most dangerous skill players in the game, quite nearly a lock to catch the ball if it’s thrown anywhere near him. He made one of his classic jump-ball touchdown catches in the second quarter, then hauled down the 15-yard throw from Costello that cut Oregon’s lead to 31-28 with 3:10 remaining.

Then there was the surprise element of Osiris St. Brown, the redshirt freshman who didn’t figure to get a lot of playing time this season. He is the older brother of Amon-Ra St. Brown, getting plenty of notice as quarterback JT Daniels’ favorite target at USC. Stanford’s St. Brown had a 53-yard reception from Costello a week earlier against UC Davis, setting up a touchdown, and when Stanford executed a brilliant three-play strike that culminated in Arcega-Whiteside’s second score, Brown struck a damaging blow on a 49-yard connection with Costello.

Finally, there is the fine tradition of Stanford tight ends, currently producing the most lethal combination in the country with 6-foot-5 Kaden Smith, who caught six passes for 95 yards, and 6-7 Colby Parkinson, who had a night to remember.

Let’s stop here to consider that second gift. Oregon didn’t merely give this game away on an ill-fated high snap. The Ducks did it again with time running out. Just when it appeared they were running out the clock, CJ Verdell tried to gain some extra yardage on a rushing play and fumbled the ball away, an inexcusable mistake that surely will torment him for days to come.

Needing three points to force overtime, Costello wasted little time, hitting Parkinson on a key third-down reception that helped set up Jet Toner’s game-tying field goal as time expired. And when the game lurched into overtime, Parkinson used all of his height to tip a Costello pass to himself, with a big left hand, and secure the touchdown catch. That’s when the Stanford secondary, brought to its knees most of the evening, stood tall to reject four consecutive pass plays.

It seems astounding that Herbert didn’t come out with a win. At one point he’d connected on 25 of his 27 passes, and it wasn’t just the numbers, but the velocity and sheer accuracy of his throws, many delivered in heavy traffic. There was a lot of Heisman Trophy talk surrounding Herbert before the game, and such platitudes will not vanish, but Herbert is also in search of his first truly signature win at Oregon.

Thanks to a wild, improbable and thoroughly memorable Stanford win, that will have to wait.

Bruce Jenkins is a San Francisco Chronicle columnist. Email: bjenkins@sfchronicle.com Twitter: @Bruce_Jenkins1