Gibraltar's Supreme Court on Thursday ruled in favour of releasing an Iranian supertanker seized on suspicion of shipping oil to Syria in breach of international sanctions, in a blow to the US which had tried to block the vessel's departure.
The Grace 1 "is no longer subject to detention", Chief Justice Anthony Dudley said in court.
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".
"There are no longer reasonable grounds to suspect that the detention of the Vessel is required," chief minister Fabian Picardo said in a statement.
Hours before the announcement, the US launched a last-minute legal move demanding that the British overseas territory detain the ship.
The move had delayed the court decision on the vessel's fate but Judge Dudley said during his ruling that he had not received a written request from US.
However, Washington can make another bid for the supertanker's detention as long as the vessel has not left Gibraltar's waters.
The Grace 1, carrying 2.1 million barrels of Iranian oil, was seized by Gibraltar police and British special forces on July 4, provoking a diplomatic crisis.
It was suspected of carrying the crude oil to war-torn Syria in violation of separate EU and US sanctions.
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".
- 'Piracy' -
Tehran repeatedly called for the release of Grace 1, insisting it had been in international waters and not headed to Syria.
Iranian authorities believe Britain detained the ship at the behest of US President Donald Trump's administration.
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, a Gibraltar government spokesman said earlier.
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, 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.
"Peace for peace and oil for oil," he said. "You cannot say that you won't allow our oil to be exported.
"It cannot be that the Strait of Hormuz is free for you and the Strait of Gibraltar is not free for us."