In one of the most surprising upsets and shocking moments in Oscar history, the poetic coming-of-age drama “Moonlight” took home the top prize for best picture at the 89th Academy Awards, beating out the heavily favored “La La Land,” which was actually announced as the winner.

The win for “Moonlight” came in a chaotic and confused moment that played out live in front of an audience of millions, as presenters Warren Beatty and Faye Dunaway initially presented the evening’s final award to “La La Land,” only to have one of the film’s producers announce that “Moonlight” had, in fact, won.

Backstage, the crew and the show’s producers were stunned when the “La La Land” producers took the stage and began to give their thank you speeches. “Oh my God, he got the wrong envelope,” said a stage hand in the wings. When “La La Land’s” Jordan Horowitz revealed the mistake — “this is not a joke, ‘Moonlight’ won” — the audience in the Dolby Theatre erupted in gasps. First-time host Jimmy Kimmel tried to deflect the moment with a reference to the crowning of the wrong Miss Universe in 2015, joking, “I blame Steve Harvey for this.”

Coming into the night with eight nominations, “Moonlight” earned three prizes in all. The story of a poor gay African-American boy growing up in Miami, the film, with a budget of well under $5 million, had been considered an underdog throughout this year’s awards season.

“All you people out there who feel like there’s no mirror for you, that your life is not reflected … we have your back,” director Barry Jenkins said, accepting the award for best adapted screenplay for the film alongside Tarell Alvin McCraney, who wrote the play on which the film was based.

Mahershala Ali also won the supporting actor prize for his role in the film as a drug dealer who takes the fatherless boy under his wing.

Three-time nominee Viola Davis also earned her first Oscar, taking home the lead actress award for her performance in Denzel Washington’s screen adaptation of August Wilson’s Pulitzer Prize-winning play “Fences.” Her win, combined with “Moonlight’s” multiple wins, contributed to a record-breaking number of awards given to black actors and filmmakers just one year after the #OscarsSoWhite controversy. In a separate first, Ali was the first Muslim actor ever to win an Oscar.

Though “Moonlight” came away the big winner, Oscar voters spread their love around to a wide variety of films, acknowledging what many have seen as a strong field of nominees.

A love letter to Hollywood that gave a modern twist to a throwback genre, Damien Chazelle’s “La La Land” did have a strong showing, winning six awards. At 32, Chazelle became the youngest person ever to win the Oscar for directing, while Emma Stone took home the lead actress award for her turn as an aspiring actress who falls in love with a jazz pianist played by Ryan Gosling.

In one of the most closely watched races, Casey Affleck won the lead actor prize for his turn as a man coping with unbearable grief in “Manchester by the Sea,” edging out two-time Oscar winner Denzel Washington, who some had expected to earn the award for his fiery performance in the drama “Fences.”

Even one-time Hollywood pariah Mel Gibson earned a measure of redemption. Though his bloody World War II epic “Hacksaw Ridge” was passed over for best picture and he failed to win for directing, his film picked up two awards for editing and sound mixing.

Throughout this year’s awards season, the conversation on red carpets and in cocktail parties has been dominated as much by political issues as by the ups and downs of the Oscar horse race, and in the weeks leading up to the show, many wondered to what extent the largely left-leaning film industry would use the occasion to further stake out its opposition to President Donald Trump.

As expected, Kimmel aimed numerous barbs at Trump throughout the night, though for the most part his digs came off as more lighthearted than caustic.

Referencing Trump’s ongoing war with the media, Kimmel wryly announced, “If you work for anything with Times in the name, even if it’s Medieval Times, we’re gonna have to ask you to leave the building. We have no tolerance for fake news here. Fake tans we love, though.”

Anticipating the president’s reaction to the evening’s proceedings, Kimmel predicted, “Some of you will get to give a speech that President Trump will tweet about in all caps during his 5 a.m. bowel movement.”

Still, in a nod to the country’s deep divisions — and Hollywood’s need to reach audiences on both ends of the political spectrum — Kimmel took a moment amid the irreverence to turn serious.

“I’m not the man to unite this country, but it can be done,” he said. “There are millions and millions of people watching right now, and if every one of you took a minute to reach out to one person you disagree with, someone you like, and have a positive, considerate conversation — not as liberals or conservatives, as Americans — if we could all do that, we can make America great again.”

One of the night’s most charged political moments came when the Iranian film “The Salesman” won the award for foreign language film. The film’s director, Asghar Farhadi, declined to attend the awards to protest the Trump administration’s travel ban, so Anousheh Ansari, the first Iranian and first Muslim woman in space, and Firouz Naderi, former director of Solar Systems Exploration at NASA, read a speech on his behalf.

“Dividing the world into the ‘us’ and ‘our enemies’ categories creates fear — a deceitful justification for aggression and war,” Farhadi wrote. “These wars prevent democracy and human rights in countries which have themselves been victims of aggression.”

But while the show was run through with political messages — a number of celebrities wore blue ribbons showing their support for the American Civil Liberties Union — the overall spirit was mostly upbeat and celebratory, giving the sense of a film industry looking to provide the nation and itself with some moments of relief amid so much political rancor.

Indeed, whenever the proceedings threatened to turn overly serious, Kimmel quickly leavened the mood, whether by dropping snacks on the crowd via miniature parachutes or bringing a group of starstruck tourists into the Dolby Theatre to take selfies with the likes of Denzel Washington, Nicole Kidman and Ryan Gosling.

After two consecutive years of back-to-back #OscarsSoWhite controversies spurred the academy to take dramatic steps to shift the demographics of its overwhelmingly white and male membership, the night served as an opportunity for the film industry to showcase diversity.

Academy President Cheryl Boone Isaacs, who helped spearhead changes aimed at doubling the number of women and minorities in the academy’s ranks by 2020, said that the film industry was “becoming more inclusive and diverse with each passing day,” adding, “Tonight is proof is art has no borders, art has no single language and art does not belong to a single faith.”

In his opening monologue, Kimmel pointedly credited Trump’s rise with helping to take the heat off of Hollywood when it comes to issues of discrimination.

“I want to say thank you to President Trump,” he said. “I mean, remember last year when it seemed like the Oscars were racist?”

In another example of the academy’s expanded inclusiveness — and the latest sign of the blurring of the boundaries between film and television — the 7 1/2 hour “O.J.: Made in America,” which ran in theaters and aired on ABC and ESPN, won the Oscar for documentary feature.

But amid the evening’s many firsts and despite the evident political fissures between Hollywood and the current administration, the night returned again and again to the power of movies and the importance of storytelling.

“There’s one place that all the people with the greatest potential are gathered, and that’s the graveyard,” Davis said, delivering one of the evening’s most impassioned acceptance speeches. “People ask me all the time: What kind of stories do you want to tell, Viola? And I say, exhume those bodies. Exhume those stories. The stories of the people who dreamed big and never saw those dreams come to fruition, people who fell in love and lost.”