The Oscars kept viewers guessing all the way to the end -- and even after it -- with “Moonlight” coming in as a surprise best picture winner after the award was mistakenly given to “La La Land” first.

The 89th annual Academy Awards got off to a rousing, musical start, with Justin Timberlake starting things off with his best song nominee, “Can’t Stop the Feeling,” making his way from the back of the auditorium to the Dolby Theater stage.

Timberlake and his dancers didn’t stay on stage for long, taking things back into the audience again to boogie with the likes of Halle Berry, Ryan Gosling and Jessica Biel before tossing things to Oscars host Jimmy Kimmel.

The first-time Oscars host wasted no time in poking fun at current events.

Get Breaking News Delivered to Your Inbox

“The country is divided right now,” he said before explaining that in the spirit of healing the nation he was going to bury the hatchet with Matt Damon. But he wasn’t so good at that, pointing out Damon’s box office disappointment with “The Great Wall,” calling it a “Chinese ponytail movie” that “lost $80 million.”

Kimmel also tried to look on the bright side of last year’s presidential election. “I want to say thank you to President Trump,” he said. “Remember last year when it seemed like the Oscars were racist? That’s gone, thanks to him.”

Keeping on the race theme, Kimmel pointed out, “Black people saved NASA and white people saved jazz,” he joked. “That’s what you call progress.”

“We don’t discriminate based on where people are from in Hollywood,” Kimmel continued. “We discriminate based on age and weight.”

After toasting “overrated” Meryl Streep for her 20th nomination, Kimmel introduced a montage of past best supporting actor winners before Alicia Vikander took the stage to present the award to this year’s winner, Mahershala Ali.

The “Moonlight” star thanked his teachers and professors. “It’s not about you,” he said they told him. “You’re in service to these stories and these characters.”

He also pointed out that his wife gave birth to their daughter four days ago.

Kimmel got in one more political dig, asking anyone from CNN, the New York Times and the L.A. Times to leave the room. “We have no tolerance for fake news. Fake tans, we love,” he explained.

Kate McKinnon and Jason Bateman then took the stage to present the Oscar for best makeup and hairstyling, which went to “Suicide Squad.”

McKinnon and Bateman stuck around to present best costume design to “Fantastic Beasts and Where to Find Them.”

“Sting told me I was going to win tonight, and I didn’t believe him at all,” Colleen Atwood said while accepting her fourth Oscar.

“Hidden Figures” stars Taraji P. Henson, Octavia Spencer and Janelle Monae, who in turn welcomed the film’s subject, 98-year-old Katherine Johnson, who received a standing ovation.

The actresses then presented the Oscar for best documentary feature, which went to “O.J.: Made in a America,” the nearly eight-hour ESPN miniseries.

Kimmel then tried an O.J. Simpson joke that drew groans from the celebrity audience. Luckily, he was able to then introduce consummate crowd-pleaser Dwayne Johnson, who sang a bit before bringing Lin-Manuel Miranda and “Moana” star Auli’i Cravalho to the stage to perform -- with Miranda offering up a new prologue rap for their nominated song, “How Far I’ll Go.”

Looking to one-up his peanut butter and jelly sandwich gag at last year’s Emmy, Jimmy Kimmel treated the crowd to candy dropped into the auditorium via small packages on parachutes.

The next awards, presented by Sofia Boutella and Chris Evans, went for sound editing and sound mixing. “Arrival” took home sound editing, while “Hacksaw Ridge” took sound mixing.

Vince Vaughn acknowledged the latest recipients of the Governor’s Awards -- including Jackie Chan -- before a highlight reel of best supporting actress acceptance speeches ushered in this year’s award, presented by Mark Rylance. This year, best supporting actress went to Viola Davis for “Fences,” her first Oscar and third nomination.

“There’s one place that all the people with the greatest potential are gathered, and that’s the graveyard,” she said from the stage, saying she wanted to exhume the stories of the people who never saw their dreams come true. “We are the only profession that celebrates what it means to live a life. So here’s to August Wilson, who exhumed and exalted the ordinary people.”

Davis tearfully paid tribute to her fellow cast members, co-star and director Denzel Washington, her parents, her sister and her husband and daughter.

“I don’t know if anybody’s going to be able to give a speech after that,” Kimmel joked after Davis’ moment. “She was just nominated for an Emmy for that acceptance speech.”

Charlize Theron and her personal inspiration, Shirley MacLaine, then took the stage to present the best foreign language film Oscar to “The Salesman,” directed by Asghar Farhadi, who notably was not present to accept the award. A statement from Farhadi was read instead.

“My absence is out of respect to the people of my country and those of the other six nations” barred entry into the U.S. “Dividing the world into the us and our enemies categories creates fear.” His statement then called on filmmakers to continue creating empathy through their work.

Dev Patel then introduced Sting’s performance of his Oscar-nominated song, “The Empty Chair.” The former Police frontman sang and played guitar on a stark, darkened stage.

Before bringing in his next stunt -- a bus full of tourists who think they’re on their way to an exhibit on Oscar gowns -- Hailee Seinfeld and Gael Garcia Bernal took the stage to present the animated film awards, starting with best animated short film, which went to Pixar’s “Piper.”

Before announcing animated feature, Bernal made an impassioned plea against President Trump’s planned wall along the Mexican border, referring to actors as “migrant workers.” Best animated feature went to Disney’s “Zootopia,” with its filmmakers thanking audiences for celebrating a film that embraces tolerance.

“Fifty Shades Darker” stars Dakota Johnson and Jamie Dornan then took the stage to present the Oscar for production design, which went to the married team behind “La La Land.”

Then it was time for Kimmel’s stunt with the tour bus riders to come to fruition. The stunned tourists couldn’t stop taking pictures with their phones. Ryan Gosling even gave one of the tourists his leftover candy, while Denzel Washington performed an impromptu wedding. Jennifer Aniston provided her sunglasses as a wedding present.

Kimmel then invited the tourists to all touch Mahershala Ali’s brand-new Oscar.

“Rogue One” stars Felicity Jones and Riz Ahmed took the stage to present the Oscar for best visual effects, which went to the team behind “The Jungle Book.”

Seth Rogen and his inspiration, Michael J. Fox, came together on stage, even stepping out of a DeLoreon. “I am at the Oscars with Michael J. Fox and a DeLoreon wearing future shoes,” Rogen said. “All I have to do now is sing the Schuyler sisters’ song from ‘Hamilton’ and my bucket list will be complete.”

Much to the delight of Lin-Manuel Miranda, he began to do just that, cajoling Fox into joining him.

They then presented the award for best editing, which went to “Hacksaw Ridge.”

Kimmel then reenacted a scene from “The Lion King” with “Lion” star Sunny Pawar before dropping another batch of candy on the audience.

Salma Hayek and David Oyelowo presented the Oscars for both documentary short subject and live action short film. In the documentary category, “The White Helmets,” about imperiled rescue workers in Syria. The live-action short film award went to “Sing.”

“We’re more than two hours into the show and Donald Trump hasn’t tweet about us once, and I’m starting to get worried about him,” Kimmel said. He then tweeted at the president, “U up?”

John Cho and Leslie Mann then took the stage to clumsily explain the Oscars’ sci-tech awards ceremony, an event “so prestigious, none of you were invited,” Mann said.

Javier Bardem and Meryl Streep then took the stage to present the award for best cinematography, which went to Linus Sandgren for “La La Land,” the musical’s second award of the night.

Kimmel then unveiled a special Oscars edition of his hit segment, Mean Tweets, featuring Natalie Portman, Ryan Gosling, Samuel L. Jackson and more. A few stars had NSFW comebacks for their critics, including Robert De Niro.

Ryan Gosling and Emma Stone then took the stage to introduce the best song nominees from their film, “La La Land,” welcoming their co-star John Legend to the stage.

Legend sang “City of Stars” from a grand piano, with dancers moving around him. As he transitioned into “Audition (the Fools Who Dream),” the dancers took the air, reliving one of the film’s more iconic moments.

After making Lin-Manuel Miranda blush and being tripped by Matt Damon, Kimmel introduced presenter Samuel L. Jackson, in attendance to announce the winner the best original score, which went to “La La Land” composer Justin Hurwitz.

Jackson’s “Avengers” co-star Scarlett Johansson then took the stage to present best original song, which brought Hurwitz back to the stage to accept the Oscar for the “La La Land” track “City of Stars.”

Jennifer Aniston then took the stage to present a longer-than-usual In Memoriam segment, with tributes to Carrie Fisher, Prince, Gene Wilder and more. She made a special mention for Bill Paxton, who’d passed away Saturday. Sarah Bareilles performed Joni Mitchell’s “Both Sides Now” as images of those the Academy lost in the last year played on screen.

Changing tone, Kimmel then presented a clip about how he’s inspired by Matt Damon’s performance in “We Bought a Zoo.” Damon and his “Good Will Hunting” co-writer, Ben Affleck, then took the stage to present the Oscar they won nearly 20 years ago, best original screenplay -- only Damon was only introduced as “and guest.” Then, whenever Damon spoke, the orchestra would attempt to play him off, with Kimmel conducting.

Once things settled down, the Oscar went to Kenneth Lonergan for “Manchester by the Sea.”

Amy Adams then took the stage in a shimmering silver gown to present best adapted screenplay to Barry Jenkins and Tarell Alvin McCraney for “Moonlight.”

Worried that the candy wasn’t doing enough to tide the crowd over, Kimmel then unleashed parachutes filled with cookies and doughnuts. “It’s like a horror movie,” he said. “Gluten is literally falling from the sky.”

Halle Berry took the stage to present the award for best director, which went to Damien Chazelle for “La La Land,” making him the youngest winner in the category in Oscar history.

Then, with another highlight reel of past speeches, it was time for the Oscar for best actor, with Brie Larson presenting the award to “Manchester by the Sea” star Casey Affleck, who gave a humbled and shy acceptance speech, thanking fellow nominee Denzel Washington for teaching him to act -- despite the fact that they met for the first time that night.

Leonardo DiCaprio then took the stage to present best actress to “La La Land” star Emma Stone. “I realize that a moment like this is a huge confluence of luck and opportunity,” she said. “I still have a lot of growing and learning to work to do, and this guy is a really beautiful symbol to continue on that journey.”

Warren Beatty and Faye Dunaway -- celebrating the 50th anniversary of “Bonnie and Clyde” -- took the stage to present best picture -- or, as Kimmel described it, “a chance to see Matt Damon lose an Academy Award.”

“Our goal in our politics is the same as our goal in art: to get to the truth,” Beatty said, adding that the nominated films “show us the growing diversity in our community and a respect for diversity all over the world.”

That final honor of the night appeared to go to the most nominated film of the night, “La La Land,” with the producers going through their acceptance speeches before it became clear that Beatty and Dunaway had actually made a mistake and “Moonlight” was the best picture winner.

“I opened the envelope and it said Emma Stone for ‘La La Land,’” Beatty explained. “I wasn’t trying to be funny.”

The team for “Moonlight” then took the stage in bewilderment. Barry Jenkins and producer Adele Romanski accepted the award while trying to make sense of what was happening to them.

“I knew I would screw this show up,” Kimmel said as he closed the show. “At least we got to see some extra speeches.”

Later, Price Waterhouse Coopers issued this statement:

“We sincerely apologize to “Moonlight,” “La La Land,” Warren Beatty, Faye Dunaway, and Oscar viewers for the error that was made during the award announcement for Best Picture. The presenters had mistakenly been given the wrong category envelope and when discovered, was immediately corrected. We are currently investigating how this could have happened, and deeply regret that this occurred.

“We appreciate the grace with which the nominees, the Academy, ABC, and Jimmy Kimmel handled the situation.”