Spanish filmmaker Isaki Lacuesta's "Los pasos dobles" beat the much-fancied Japanese film "Kiseki" to the top prize at the San Sebastian festival on Saturday.
The jury, chaired by Oscar-winning American actress Frances McDormand ("Fargo"), named Greek-born Filippos Tsitos as best director for his thriller "Adikos kosmos" (Unfair World).
The special jury prize went to French actress and director Julie Delpy for her film "Le Skylab", a tribute to her late mother.
The award of the top prize to "Los pasos dobles" (The Double Steps) was greeted with surprise.
The film had divided critics and "Kiseki" (I Wish), directed by Hirokazu Kore-eda, had been tipped as favourite to win. The film focuses on the tsunami that hit Japan in March.
In the end, the Japanese film had to settle for the prize for best screenplay.
"Los pasos dobles" takes its inspiration from the biography of French artist and author Francois Augieras (1925-1971) who painted the walls of a military bunker in the desert and let it sink into the sand.
Delpy's film "Le Skylab", a tribute to her late mother, is a humorous family chronicle set against the backdrop of her grandmother's birthday in 1979, and got one of the warmest receptions of the festival at its screenings.
The autobiographical screenplay focuses on the divergent political beliefs in the family and the first loves of some younger members of the family.
"Adikos kosmos" (Unfair World) is the story of a policeman who helps delinquents until the day he finds himself implicated in a murder case and starts to question his ideas of justice.
As well as the director's prize, the film earned its star, Antonis Kafetzopoulos, the the award for best actor.
The best actress prize went to Spain's Maria Leon for her role in "La Voz Dormida" (The Sleeping Voice) by Benito Zambrano, a drama set in Franco-era Spain.
She plays a young woman who must help her sister who is locked up in a jail awaiting execution once she has given birth to her baby.
A total of 16 films were in the official competition.
The festival opened with a screening out of competition of a supernatural thriller "Intruders", starring British actor Clive Owen and directed by Spain's Juan Carlos Fresnadillo ("Intacto", "28 Weeks Later").
American actress Glenn Close received a special prize honouring her career in cinema.
She took the opportunity to present her new film, "Albert Nobbs", which she also co-wrote with Booker Prize-winning novelist John Banville.
Adapted from short story by Irish writer George Moore, it tells the story of a woman who disguises herself as a man in 19th-century Ireland to get a job as a butler in an up-market hotel.
Close, who plays the title role, also produced the film.
She and Clive Owen were among the few big names at the cash-strapped 59th edition of the festival, which said it had been forced to weigh the cost of each invitation.
Sponsorship income had climbed by just over 300,000 euros this year, boosting an overall budget of more than seven million euros.