Super Bowl MVP Tom Brady and the Fox broadcast network weren’t the only big winners Sunday night. The online lender Social Finance Inc.--known as SoFi--scored a last-second touchdown as well.

Before the game, SoFi had inked a deal with Fox to run a Super Bowl ad if--and only if--the matchup went into overtime. No Super Bowl in NFL history had ever extended beyond regulation play.

SoFi said the ad option cost less than half of the roughly $5 million that Fox was charging for 30 seconds of commercial time during the game’s four quarters.

The company was one of at least four advertisers who committed upfront to buy these discounted ads. Hulu and Sprint ran ads during regulation, but also negotiated for overtime slots. Like SoFi, Proactiv, a direct marketer that sells acne-treatment products, only negotiated for an ad using the overtime option and didn’t air a commercial during regulation. A representative for Hulu declined to elaborate, and representatives Proactiv couldn’t be reached for comment.

The way these deals work is that brands agree to authorize the ad buys, and Fox runs the ads if the game goes into overtime. If overtime never happens, no money ends up changing hands.

As Super Bowl LI turned from what seemed on pace to be a blowout by the Atlanta Falcons into an epic comeback by the New England Patriots to force the game beyond regulation play, the marketers suddenly found themselves airing commercials to a captive audience of more than 100 million.

SoFi only started thinking about buying a Super Bowl ad a few weeks ago, said the company’s Chief Operating Officer Joanne Bradford.

Last year, SoFi ran a Super Bowl ad that showed shots of random people doing everyday tasks as a narrator points out if the person is “great” or “not great.” The ad painted SoFi as an exclusive club and was widely accused of being mean spirited. Plus, its website was flooded with applicants after the ad ran and SoFi couldn’t meet the surge in demand.

This time around, “I thought, we don’t have creative or the time,” Ms. Bradford said. “We were focused on March Madness.”

But a colleague proposed buying an ad that would run only in the event of overtime play. When broadcasting the Super Bowl, Fox had made it a habit to sign deals with a handful of advertisers who agreed to run ads if overtime indeed happened.

Ms. Bradford and her team decided to put together an ad for the game using images of the online lender’s customers--essentially user-generated content-- and focused on the theme of real people paying off loans and financing homes with the help of SoFi. The company’s in-house designer partnered with a copywriter and produced the ad for less than $10,000, Ms. Bradford said. It was delivered to Fox over the weekend.

Late in the first half, when Atlanta was leading New England 28 to 3, Ms. Bradford said she was at home emailing colleagues, saying that the bargain ad was probably never going to run.

“I was like whatever, forget it,” she said. “It was a great idea and no one will ever know about it.”

Then, as the Patriots began their furious rally in the second half, Ms. Bradford and her team started to realize “this is gonna happen,” she said. They started galvanizing the company’s workforce, including making sure the website had enough server capacity and telling people who man SoFi’s call centers to get into the office.

Meanwhile, Fox’s sales team was covered for the ad break before the start of overtime and an additional break, if needed. But in case the game went longer, the company needed more advertisers, said a person familiar with the matter.

Fox’s sales executives, many of whom were at the game, started calling ad buyers while also trying to corral marketers who were also in Houston to see if they wanted to buy ads that would run in the event of an extended overtime, the person said.

“It was the most adrenaline-filled 15 minutes of my career,” said Bruce Lefkowitz, executive vice president of ad sales for Fox Networks Group, who missed the end of the game. “Live sports prepare you for the unexpected, but nothing compares to the moment.”

In the end, the extra commercials weren’t needed, as the Patriots scored on their opening drive during overtime to clinch the team’s fifth Super Bowl win.

For SoFi, the ad bet caused traffic to spike “100X,” said Ms. Bradford. “It was amazing.

21st Century Fox, the owner of the Fox broadcast network, and News Corp, parent company of The Wall Street Journal, share common ownership.

Write to Mike Shields at mike.shields@wsj.com