It is one of the greatest underwater treasure troves of all time, a glittering haul of gold and silver recovered from a mysterious sunken Spanish galleon and secretly flown across the Atlantic to the US.

But now an epic battle over ownership of 594,000 gold and silver coins scattered on the ocean floor has ended with victory for the Spanish government, with the American treasure-hunter Odyssey Marine Exploration ordered to send the valuable haul back home.

A jubilant Spanish government announced on Wednesday that the $500m-worth (£308m) of gold and silver coins found at a site that Odyssey called "Black Swan" would be back on Spanish soil within 10 days.

"This sentence gives Spaniards back what was already theirs," said the culture minister, José Ignacio Wert. "There is a space of 10 days in which the coins must be returned."

The court decision puts an end to nearly five years of intrigue on the high seas since Odyssey scooped the precious haul from the Atlantic seabed in May 2007. To the fury of Spanish authorities it secretly landed the trove in Gibraltar and flew it out in chartered aircraft to its base in Florida.

A American circuit court judge has upheld a decision by Atlanta judge Mark Pizzo, who had declared the trove came from the Nuestra Señora de las Mercedes, a Spanish frigate sunk by a British squadron off Cape St Mary, Portugal, in October 1804.

The judge rejected Odyssey's argument that there was no clear wreck site, with the coins scattered so widely it was impossible to say exactly which vessel they came from.

The treasure-hunting company, he said, had set out to find the Mercedes and had clearly done so.

Pizzo argued that the coins, all dated prior to 1804, matched the Mercedes' cargo of coins minted in Lima, Peru – part of a haul being brought back to finance Spain's European wars. He also said cannon found there matched those on the Mercedes.

Rightful ownership of the coins, which fill 600 barrels, now belongs to Spain and to descendants of the 250 Spanish sailors who were lost when the vessel blew up.

However, the whereabouts of a further 400,000 coins from the Mercedes remains a mystery.

The treasure was found by one of Odyssey's remote-controlled, deep sea robots as it scoured the seabed 1,100 metres down.

Odyssey's decision to use Gibraltar led to a tense stand-off in disputed waters off the rock. Shortly after the coins had been spirited off to Florida, a Spanish warship forced the company's 250ft Odyssey Explorer salvage vessel into the nearby Spanish port of Algeciras, while it was searched. Its captain, Sterling Vorus, was arrested, but later freed.

The Wikileaks release of state department cables revealed that US diplomats had offered to side with Spain against Odyssey. The US ambassador in Madrid sought to tie the treasure to attempts by an American citizen, Claude Cassirer, to recover a painting by Camille Pissarro that hangs in Madrid's Thyssen-Bornemizsa museum.

"It was in both governments' interest to avail themselves of whatever margin for manoeuvre they had, consistent with their legal obligations, to resolve both matters in a way that favoured the bilateral relationship," Aguirre told the then culture minister, César Antonio Molina, in July 2008.

In contrast to its battles with Spain, Odyssey has done deals with the British government to recover and share sunken treasure.

An agreement to excavate a wreck thought to be the 80-gun warship HMS Sussex, which sank in 1694 carrying up to 10 tonnes of gold, led to complaints that a unique heritage site was being despoiled. The Sussex and 12 ships in its fleet sank in storms in 1694 while on a secret mission to bribe the Duke of Savoy to act as an ally in a war against Louis XIV of France.

"The whole arbitration process is still not one that gives us confidence in what ministers have told us, which is that the archaeological issues are paramount." said George Lambrick, director of the Council for British Archaeology, adding that serious concerns remained.

Neil Cunningham-Dobson, a British archaeologist who led initial examinations, denied the company would spoil the site: "Odyssey are one of the best and most reputable firms in the business and use the latest technologies."

Wert said some of the coins would be distributed to Spanish museums. "We want people to see them," he said.

He admitted the only stumbling block to the immediate return of the coins might be a further appeal by Odyssey, if it was accepted by a higher court and a suspension order placed on the sentence.

A spokeswoman for Odyssey, Laura Barton, indicated an appeal might be forthcoming. "Currently, there is no final order from the court to give the Black Swan coins to Spain," she told the Guardian without giving further details.

"It is certainly reasonable to assume that should the cargo recovered by Odyssey be transferred to Spain, it will never be returned," the exploration company had argued before the appeals court.