Tehran (AFP) - Iran's top diplomat said a US attempt at "piracy" had failed after a Gibraltar court on Thursday ordered the release of a tanker carrying Iranian oil despite a US detention request.

A supreme court judge in Gibraltar ruled in favour of releasing the Grace 1 supertanker which had been seized in waters off the British territory on July 4 on suspicion of breaching EU sanctions on Syria.

"Having failed to accomplish its objectives through its #EconomicTerrorism -- including depriving cancer patients of medicine -- the US attempted to abuse the legal system to steal our property on the high seas," Iranian Foreign Minister Mohammad Javad Zarif tweeted.

"This piracy attempt is indicative of Trump admin's contempt for the law."

Iran's ambassador to London called the verdict a "humiliating defeat" for the United States, his country's arch-enemy.

"Minutes ago... the tanker carrying Iranian oil was freed from illegal detention," Hamid Baeidinejad wrote on his Twitter account.

"The US with its last minute cowardly attempts sought to stop the tanker leaving detention which faced a humiliating defeat," he added.