Miss Colombia Ariadna Gutierrez thought she had won the Miss Universe pageant.
But then the host Steve Harvey said he'd made a terrible mistake.
"Okay, folks," he said. "I have to apologize."
Miss Colombia was the first runner-up, he said, holding up the card that contained the official results.
The real winner was Miss Philippines, Pia Alonzo Wurtzbach, who looked astonished by the sudden turn of events.
The almost unbelievable ending to the pageant was televised live in many countries, including on the Fox network in the United States on Sunday night.
Wurtzbach covered her mouth in shock when Harvey announced the correction. Then she walked out to the front of the stage and waited for Gutierrez to hand over the crown.
Harvey stood to the side, visibly embarrassed, while the crowd cheered.
He called it a "horrible mistake."
And many others agreed; social networking sites lit up with instant reaction. ESPN's Darren Rovell called it "the worst mistake in TV awards show history."
Afterward, Harvey tweeted, "I'd like to apologize wholeheartedly to Miss Colombia & Miss Philippines for my huge mistake. I feel terrible."
He followed up and said, "Secondly, I'd like to apologize to the viewers at that I disappointed as well. Again it was an honest mistake."
Some people quickly came to Harvey's defense; Earvin Magic Johnson tweeted, "we've all made mistakes in our careers & lives." He encouraged Harvey to "keep the faith and keep going strong!"
But others made plenty of jokes at Harvey's expense.
The screw-up happened at an important moment of transition for Miss Universe. The pageant was owned by Donald Trump and NBCUniversal until earlier this year. When the business partners split up in the midst of a Trump campaign controversy, the giant talent agency WME/IMG took control of the pageant.
If nothing else, the mistake provided the pageant with a sudden jolt of attention.
Vulture's Joe Adalian, a veteran television industry reporter, said it "wouldn't shock me" if the crown switcheroo was actually a planned stunt. "Everybody wins here," he said.
Trump, through his Twitter account, had been touting the pageant's new owners earlier in the evening.
After the error, he retweeted a fan who asked, "Where's @realDonaldTrump when you need em?"
By Brian Stelter