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".
“In light of the assurances we have received, there are no longer any reasonable grounds for the continued legal detention of the Grace 1,” chief minister of Gibraltar, Fabian Picardo, said in a statement on Thursday.
The supertanker, carrying 2.1 million barrels of Iranian oil, was seized by Gibraltar police and British special forces on 4 July, 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 19 July in the strategic Strait of Hormuz -- the conduit for much of the world's crude -- for breaking "international maritime rules".
The two tankers have become pawns in the standoff between Iran and the West, with Tehran accusing Britain of detaining the ship at the behest of the Trump administration.
It was not immediately clear whether the release of Grace 1 would pave the way for a possible swap for the Stena Impero tanker.
When asked in a CNN interview where the Grace 1 was now headed, chief minister Picardo said, “That is not an issue for the authorities in Gibraltar.”
Following Thursday's decision, Iran's foreign minister said a US attempt at "piracy" had failed.