In an unprecedented onstage faux pas, the 2017 Oscars broadcast Sunday night bumbled its all-important best picture award.

Presenters Warren Beatty and Faye Dunaway opened the envelope, announced the long-expected victory of the cheerful song and dance romance “La La Land,” and welcomed the film’s production team onstage to celebrate a win that seemed almost inevitable.

Then, in the most unusual reversal in the show’s 89-year history, the acceptance speeches were halted. "La La Land" producer Jordan Horowitz, informed by an Oscars staffer that there had been a mistake, announced that the actual winner was the low-budget African-American indie drama “Moonlight” in an upset.

Beatty apologetically explained that the error had arisen when he and Dunaway were given the wrong envelope — one listing “La La Land” star Emma Stone, who had just won best actress. Beatty, apparently confused because he was holding the wrong card, searched the envelope twice for another card, and then handed it off to Dunaway who quickly read the wrong winner.

The “La La Land” team graciously handed over the award, an act saluted by “Moonlight” writer-director Barry Jenkins in his acceptance speech.

“Very clearly, even in my dreams this could not be true, but to hell with dreams!” he said. “I’m done with it because this is true. Oh my goodness. I have to say it is true, it’s not fake. We’ve been on the road with these guys for so long and that was so gracious, so generous of them. My love to ‘La La Land,’ my love to everybody.”

Despite the confusion at the end of the show, “La La Land,” with a record-matching bouquet of 14 nominations, earned six awards, more gold statuettes than any other film.

Stone’s best actress award proved that Hollywood’s love affair with its own stories are irresistible to Oscar voters. Damien Chazelle, 32, made Oscar history as the youngest winner of the best director trophy. “City of Stars,” the film’s melodious earworm of a ballad won its best song statuette after the musical romance won best cinematography, production design and original score.

But films didn’t have to be full of song, dance and romance to be rewarded. The emotionally wrenching family drama “Manchester by the Sea” earned Casey Affleck his first best actor Oscar for his role as a man whose soul has been torn apart by personal tragedy. It also handed Kenneth Lonergan his first screenwriting Oscar for a story that combined everyday reality with touching poetry.

Until the confused finale, the rebooted 2017 Oscars had been a night of red carpets, blue ribbons and nonwhite talent. Seven of the 20 acting nominees were nonwhite, compared to zero the past two years. And winners among them quickly emerged.

For the first time, both of the winners in supporting roles were African American. Mahershala Ali won his first Academy Award to a standing ovation as best supporting actor for “Moonlight.” As Juan, a compassionate Miami drug dealer, Ali’s character takes under his wing a bullied boy growing up poor and sexually uncertain. “At some point, you got to decide for yourself who you’re going to be,” Juan tells him. “Can’t let nobody make that decision for you.”

With that expression of pride and rejection of outdated expectations, Juan and Ali became early ambassadors for Hollywood’s redoubled efforts to recognize and reward diversity following last year’s the #OscarsSoWhite controversy. Ali is the first Muslim actor to win an Academy Award, and “Moonlight” is the first film to have more than three black Oscar winners.

Viola Davis, in her third acting nomination, won her first Oscar as a long-suffering wife of a Pittsburgh garbage man in the film adaptation of August Wilson’s play “Fences.”

In an emotionally charged speech, she called for films about lives that are often ignored, “the stories of people who dreamed big and never saw those dreams to fruition ... So here’s to August Wilson, who exhumed and exalted the ordinary people.”

With the nation’s political tempers running at fever pitch, there was speculation about whether the show would achieve a likable party atmosphere or fire slings and arrows at Washington, D.C. Pointed comments were made by winners in minor and technical categories. But winners in most of the major categories shied away from politics.

At Saturday’s Film Independent Spirit awards show, where Affleck won as best male lead, he used his moment at the microphone to denounce Trump’s “abhorrent policies.” At the Oscars, however, he remained nonpolitical. So did Meryl Streep, whose sustained attack on then-President-elect Trump at the Golden Globes earned her onstage praise from first-time host Jimmy Kimmel. As one of the Oscars presenters, she kept mum.

The ceremony was not without its moments of surprise. You would not have expected before Sunday to say “the Oscar-winning ‘Suicide Squad’” or “Fantastic Beasts and Where to Find Them,” but now you can.

By and large, however, the show stayed close to its progressive script. Best picture nominations promoted diversity, with actors of color starring in four of the nine competing films: “Fences,” “Hidden Figures,” “Lion” and “Moonlight,” which also won for best adapted screenplay.

For the first time, four black directors were among the nominees in the best documentary feature category. Three of them made films dealing with race relations in America, including Ava DuVernay’s “13th” about racial disparities in mass incarceration in the United States and Raoul Peck’s “I Am Not Your Negro,” a biography of American race relations as interpreted by the late African-American writer and social critic James Baldwin.

The prize went to director Ezra Edelman, whose “O.J.: Made In America” explores the racial history of Los Angeles, its police department’s record of violence and racial disparities in the criminal justice system. At 7 hours and 47 minutes, it is the longest film ever to win an Oscar. Edelman accepted his award in part on behalf of “victims of police brutality, racially motivated violence and criminal injustice.”

There were nods for international diversity as well. “This is for all the immigrants,” said Italian-born Alessandro Bertolazzi, accepting his Oscar for best achievement in makeup and hairstyling on the comic-book adventure “Suicide Squad.”

Iranian director Asghar Farhadi won his second foreign-language Oscar for “The Salesman,” but asked to be represented by Iranian-American astronaut Anousheh Ansari. Farhadi announced in January that he would not attend because of President Trump’s attempt to ban Syrian refugees and citizens from seven predominantly Muslim countries.

In his acceptance speech, read by Ansari, he said, “My absence is out of respect for the people of my country and those of [the] other six nations who have ben disrespected by the inhumane law that bans entry of immigrants to the U.S. Dividing the world into the us and the enemies categories creates fear. ... Filmmakers can turn their cameras to capture shared human qualities and break stereotypes of various nationalities and religions. They create empathy between us and others — an empathy we need today more than ever.”

The red carpet style of the evening was a small slice of blue ribbon. Many attendees at Sunday night’s gala wore the badges, symbolizing support for the American Civil Liberty Union’s defense of legally guaranteed individual rights and liberties.