The coming of age film about Chiron, a young black man growing up in Miami, also won the supporting actor and screenplay awards.
While announcing the top honor of the evening, an apparent mix-up of cards created unprecedented confusion in which Faye Dunaway and Warren Beatty initially declared La La Land the winner, rectified only after a backstage intervention.
Beatty apologized as the Moonlight cast and producers accepted the award.
Considered the favorite, La La Land, about a young couple in Hollywood -- a jazz artist and an aspiring actress -- bagged six wins from a record 14 nominations.
The wins included 32-year-old director Damien Chazelle, who became the youngest ever person to win the category.
Emma Stone was named best actress, her first Oscar after two nominations and she thanked Ryan Gosling “for being the greatest partner on this crazy adventure”.
The film grabbed Oscars for original score, song, production design and cinematography and had previously won a record-high seven Golden Globes and five Baftas while grossing more than 10 times its $30 million budget.
The top actor award went to Casey Affleck for his role in Manchester by the Sea, a movie depicting a soured and irascible handyman whose life is changed after he becomes the guardian for his 16-year-old nephew.
Joining his Oscar-winning brother Ben, Affleck received a standing ovation as he collected his statuette.
Foreign language films
The night was punctuated by political jabs at President Donald Trump over his immigration policies and memorable moments on race relations -- a topic that dominated the scene after last year’s ceremony.
In his opening presentation, host Jimmy Kimmel wasted no time in going after Trump. “I want to say thank you to President Trump. I mean, remember this time last year when it seemed that the Oscars were racist?”
Before the prime time broadcast, six foreign language film nominees issued a statement that condemned Trump’s executive actions banning nationals of several Muslim-majority countries from entering the U.S.
Bentley Dean, Australian co-director of Tanna, said he was happy to see “thousands of people on the streets protesting” against “some mad man coming up with some racist, stupid idea”.
After last year’s “#OscarsSoWhite” controversy in which no black or minority actors were nominated in major categories, the night’s first award went to Mahershala Ali for his supporting role in Moonlight.
He became the first Muslim to win an Oscar for his performance as mentor and drug dealer Juan, for which he has won universal praise.
Ali thanked his teachers and those close to him and nodded to his wife and 4-day-old daughter.
Viola Davis was the second black actor to win a major award, taking the supporting actress category for her critically acclaimed work in Fences.
The win made Davis the first black to win an Oscar, Emmy and Tony award -- a feat achieved by less than two dozen actors in history.
White Helmets
Katherine Johnson, 98, one of NASA’s black female mathematicians the film Hidden Figures is based on, was brought out in a wheelchair and honored with a standing ovation. The movie received three nominations but failed to take home Oscar gold.
The Salesman, which tells the story of a young couple in Tehran whose lives are changed by the previous tenants of their new home, won the foreign language award.
Anousheh Ansari, the first female space tourist, read a letter from director Asghar Farhadi, who did not attend. Iran was one of the countries blacklisted in Trump’s ban.
“I am sorry I am not with you tonight,” Farhadi said. “My absence is out of respect for the people of my country and those of the six other nations who have been disrespected by the inhumane law that bans the entry of immigrants to the U.S.”
Dividing the world into “us and our enemies” creates a fear that is used to justify wars, he added. Filmmakers can help create empathy to bridge the gap -- an empathy “we need today more than ever”.
The White Helmets, another Oscar-worthy production impacted by the ban, won in the documentary short category.
The film about Syria’s volunteer rescue workers was missing its cinematographer, Khaled Khateeb, who could not attend the ceremony because his country, too, is blacklisted.
As he accepted the trophy, director Orlando von Einsiedel quoted Raed Saleh, who heads the White Helmets group.
“Our organization is guided by a verse from the Quran: ‘To save one life is to save all of humanity.’ We have saved more than 82,000 civilian lives. I invite anyone here who hears me to work on the side of life, to stop the bloodshed in Syria and around the world.”