The 64-year-old Panahi, one of Iran's most award-winning filmmakers, has been imprisoned in his home country twice and was forbidden for many years to leave Iran or make films at all.
In his acceptance speech, Panahi urged all Iranians around the world to unite and fight together for a "free Iran".
The most important thing right now is our country and our country's freedom, he said.
"It was just an accident" tells the story of two former Iranian prisoners who meet a man they believe was tortured by in prison. The film was shot in complete secrecy and has been partially inspired by his own experiences.
Grand Prix to Trier
Norwegian director Joachim Trier's film "Sentimental value" starring Stellan Skarsgård in the lead role received the jury's Grand Prix.
I'm incredibly happy about this prize. I come from a small country where we make films that try to reach out, he said after the award ceremony.
Skarsgård, who is also one of the film's producers, plays a former successful film director who has written a screenplay that could be his comeback, and he wants his daughter Nora (Renate Reinsve) to play the lead role in it.
Stellan has warmth, unlike the character in the film. I have admired Stellan for years, I wrote the role for him, says Trier.
Cruise added glamour
Tarik Saleh's film "Eagles of the Republic", the final part of his so-called Cairo trilogy, went without a prize at the gala.
French film star Juliette Binoche was the chairman of this year's festival jury, along with, among others, American actress Halle Berry, Jeremy Strong from "Succession", and Indian filmmaker Payal Kapadia.
The festival also got extra international glamour in the form of Cruise, whose latest – and last – "Mission impossible" film "The final reckoning" had its world premiere at the festival and was shown out of competition.
Kristina Erkenborn/TT
Gunnar Rehlin/TT
Facts: Cannes Festival winners in selection
TT
Golden Palm: "It was just an accident" by Jafar Panahi Grand Prix: "Sentimental value" by Joachim Trier
Jury Prize: "Sirat" by Oliver Laxe, "The sound of falling" by Mascha Schilinski
Jury's Special Prize: "Resurrection" by Bi Gan
Best Director: Kleber Mendonça Filho for "The secret agent"
Best Actor: Wagner Moura in "The secret agent"
Best Actress: Nadia Melliti in "La petite dernière"
Best Screenplay: Jean-Pierre and Luc Dardenne "Young mothers"
Best Film in the Un certain regard section: "The mysterious gaze of the flamingo" by Diego Céspedes
Camera d'Or (Best Debut Film): Hassan Hadi for "The president's cake"
Honorary Golden Palm: Robert De Niro, Denzel Washington
Best Short Film: "I’m glad you’re dead now" by Tawfeek Barhom