Media playback is unsupported on your device Media caption The moment La La Land's producers realised Moonlight had won best picture

Moonlight has won best picture at the Oscars - but only after an error saw La La Land initially declared the winner.

The La La Land producers were in the middle of their acceptance speeches when the mistake was discovered.

PriceWaterhouseCoopers, the accountancy firm responsible for counting the ballots, apologised for the mix-up.

La La Land still ended up the biggest winner of the night, taking home six Oscars including a best actress award for Emma Stone.

Media playback is unsupported on your device Media caption Barry Jenkins reacts to winning the Oscar

Moonlight's surprise best picture win took its haul to three, the low-budget film having earlier won the adapted screenplay award and a best supporting actor prize for Mahershala Ali.

Casey Affleck was named best actor for Manchester by the Sea, while Viola Davis was named best supporting actress for Fences.

Damien Chazelle, La La Land's 32-year-old director, became the youngest film-maker to win the best director Oscar.

Image copyright Getty Images Image caption It was all smiles for the acting winners backstage

Acting winners

Casey Affleck - best actor, Manchester by the Sea

Emma Stone - best actress, La La Land

Mahershala Ali - best supporting actor, Moonlight

Viola Davis - best supporting actress, Fences

Read the best bits from the speeches

Yet this year's ceremony will be remembered for its closing moments, when Warren Beatty and Faye Dunaway arrived on stage to announce the best picture winner.

Beatty had been mistakenly handed the previous winner's envelope, containing a card that said Emma Stone had won best actress for La La Land.

Image copyright Kevin Winter Image caption Beatty looked unsure what to do when he opened the envelope

It was this card that Dunaway read from, mistakenly declaring La La Land to be best picture and creating what Stone later described as "the craziest Oscar moment of all time".

In a statement, PriceWaterhouseCoopers (PwC) "sincerely" apologised for the error, which it said was down to the presenters having "mistakenly been given the wrong category envelope".

"We are currently investigating how this could have happened, and deeply regret that this occurred."

Host Jimmy Kimmel said he blamed Steve Harvey for the error - a reference to Harvey's mistake in announcing the Miss Universe winner in 2015.

Image copyright Reuters Image caption Emma Stone (background, left) looked shocked at events

It was La La Land producer Jordan Horowitz who announced there had been a mistake.

"This is not a joke," he told the audience, showing the correct card. "Moonlight is best picture."

Beatty returned to the microphone to explain he had been given an envelope with Emma Stone's name inside.

"That is why I took such a long look at Faye," the veteran actor continued. "I wasn't trying to be funny."

Image copyright AFP/getty Image caption Moonlight's Barry Jenkins and La La Land's Jordan Horowitz embrace after the mistake is rectified

"Very clearly even in my dreams this can't be true," said Moonlight director Barry Jenkins. "But to hell with it because this is true. It's true, it's not fake."

Paying tribute to the team behind La La Land for the way they handled the mistake, he said: "We have been on the road with these guys and it was so gracious and so generous of them."

Media playback is unsupported on your device Media caption Tom Brook looks at Moonlight, a film about the coming-of-age of a gay, black man

Speaking backstage, Emma Stone said it had been "an amazing thing to hear La La Land" named best picture.

"But we are so excited for Moonlight," she continued. "I think it is one of the best films of all time.

"Is that the craziest Oscar moment of all time? Cool! It's going to be history."

Image copyright Reuters Image caption The Moonlight director later watched the best film Oscar engraved

La La Land's other Oscars came for its cinematography, score and production design.

The Hollywood musical romance also won the best original song award for City of Stars, beating another song from its soundtrack in the process.

Kenneth Lonergan received the best original screenplay prize for Manchester by the Sea, which he also directed.

There were two awards too for Mel Gibson's war film Hacksaw Ridge, which was recognised for its editing and its sound mixing.

Follow our Oscars board on Pinterest

Follow us on Facebook, on Twitter @BBCNewsEnts, or on Instagram at bbcnewsents. If you have a story suggestion email entertainment.news@bbc.co.uk.