Papua New Guinea's Supreme Court has ruled that Australia's detention of asylum seekers on Manus Island is unconstitutional.
The ruling could jeopardise Australia's divisive policy of refusing to accept any asylum seekers who try to reach its shores by boat.
Australia pays Papua New Guinea and the tiny Pacific island nation of Nauru to hold them in detention camps instead.
The court dubbed the detention of the asylum seekers a violation of their constitutional right to personal liberty.
The court ordered both countries' governments to take immediate steps towards ending the detention of the 900 men being held at the facility.
"Treating those required to remain in the relocation centre as prisoners irrespective of their circumstances or their status save only as asylum seekers, is to offend against their rights and freedoms as guaranteed by the various conventions on human rights," the court wrote in its ruling.
Lawyer Loani Henao, who lodged the court action on behalf of Papua New Guinea's former opposition leader Belden Namah, said the decision means the centre must be shut down immediately and the asylum seekers set free.
"They were in jail against their own will for no reason," Mr Henao said. "The government of Papua New Guinea and Australia, for that matter, were not allowed to do that under our constitution."
Australian immigration minister Peter Dutton said the ruling had done nothing to change Australia's stance on asylum seekers who arrive by boat.
"No one who attempts to travel to Australia illegally by boat will settle in Australia," he said in a statement.
"Those in the Manus Island Regional Processing Centre found to be refugees are able to resettle in Papua New Guinea.
"Those found not to be refugees should return to their country of origin."
Human rights groups and refugee advocates have been fiercely critical of Australia's tough asylum-seeker policies, which the government enacted in a bid to stop migrants from boarding boats and attempting the dangerous ocean crossing from Indonesia to Australia.
Australia was also criticised in 2014 after signing a four-year, 55 million Australian dollar (£29 million) deal that offered refugees stuck on Nauru the option of being resettled in Cambodia.
Only five refugees opted to go to Cambodia and several of them subsequently chose to return to their home countries, prompting critics to dub the deal a pricey flop.