Gibraltar's Supreme Court on Thursday ruled in favour of releasing an Iranian supertanker seized on suspicion of shipping oil to war-torn Syria in breach of international sanctions, in a blow to the United States which had tried to block the vessel's departure.
The decision came after Gibraltar's government said it had received written assurances from Iran that the Grace 1 would not be headed for countries "subject to European Union sanctions".
The Grace 1 "is no longer subject to detention", said Chief Justice Anthony Dudley.
Gibraltar chief minister Fabian Picardo hailed the ruling, saying in a statement: "We have deprived the Assad regime in Syria of more than $140 million worth of crude oil."
Hours before the announcement, the US launched a last-minute legal move demanding that the British overseas territory detain the ship.
Judge Dudley said the ship would have been on its way had Washington not intervened, at a time when an accord between London and Tehran had seemed within reach after weeks of diplomatic wrangling.
Picardo said he had met with Iranian officials on July 19 in London "with a will for a de-escalation with regard to all the various problems arising from the detention of the Grace 1".
Washington's move had delayed the court decision on the vessel's fate but Judge Dudley said in his ruling that he had not received a written request from the US.
Washington can still make another bid for the supertanker's detention as long as the vessel has not left Gibraltar's waters.
Gibraltar police and British special forces seized the Grace 1, carrying 2.1 million barrels of Iranian oil, on July 4, provoking a diplomatic crisis.
The Gibraltar government on Thursday reiterated its conviction that the ship was carrying the crude to Syria in violation of separate EU and US sanctions.
The boat's navigation plan "showed a fully marked-out route" from the Gulf to the Syrian port of Baniyas, it said.
Tehran retaliated by seizing a British tanker, the Stena Impero, on July 19 in the strategic Strait of Hormuz -- the conduit for much of the world's crude -- for breaking "international maritime rules".
Britain on Thursday reiterated its demand that Iran release the Stena Impero.
"There is no comparison or linkage between Iran's unacceptable and illegal seizure of, and attacks on, commercial shipping vessels in the Strait of Hormuz and the enforcement of EU Syria sanctions by the government of Gibraltar," a Foreign Office spokesman said in a statement.
Tehran had repeatedly called for the release of Grace 1, insisting it had been in international waters and not headed to Syria.
In early July, Spanish Foreign Minister Josep Borrell said the ship was detained at the behest of the United States.
Following Thursday's decision, Iran's foreign minister said a US attempt at "piracy" had failed.
"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," Mohammad Javad Zarif tweeted.
"This piracy attempt is indicative of Trump admin's contempt for the law."
The captain and three officers from Grace 1 had their police bail lifted and were formally released without any charges.
After Thursday's court ruling, the US State Department threatened a visa ban on the crew, warning that members of any vessels assisting Iran's Revolutionary Guards -- considered a terrorist group by Washington -- in transporting oil risked being ineligible for admission to the United States.
The capture of the tankers heightened tensions just as European nations scramble to try to save a landmark nuclear deal with the Islamic Republic after the US pulled out of the accord in May last year and started imposing sanctions on Iran.
Iran responded by suspending some of its commitments under the nuclear deal.
The situation threatened to spiral out of control with ships attacked, drones downed and oil tankers seized.
At the height of the crisis, President Donald Trump called off air strikes against Iran at the last minute in June after its forces shot down a US drone.
President Hassan Rouhani said on Tuesday that Iran favours talks with the US if it lifts sanctions against the Islamic republic.
"You cannot say that you won't allow our oil to be exported," he said.
"It cannot be that the Strait of Hormuz is free for you and the Strait of Gibraltar is not free for us."