Papua New Guinea's Supreme Court has ruled that the detention of asylum seekers and refugees on Manus Island is unconstitutional.
Australia sends asylum seekers to the Manus Island detention centre under its offshore processing policy.
But the five judges on the Supreme Court's bench ruled the camp breached section 42 of the constitution, which guarantees personal liberty.
The court said "all steps" should be taken to end the "illegal" detention.
Currently around 850 men are held on the island, around half of whom have been judged genuine refugees.
'Breach of rights'
Papua New Guinea's constitution guarantees personal liberty for all people, except in defined circumstances relating to crime, illegal immigration and quarantine.
In 2014, Papua New Guinea's government amended section 42 of the constitution to add a paragraph that allowed for "holding a foreign national under arrangements made by Papua New Guinea with another country".
But the Supreme Court ruled this amendment was unconstitutional, as it did not meet a requirement to respect "the rights and dignity of mankind".
It said that because the asylum seekers and refugees had not voluntarily entered Papua New Guinea, the situation of illegal immigration did not apply to them.
"The detention of the asylum seekers on Manus Island in Papua New Guinea… is unconstitutional and illegal," it said.
"Both the Australian and Papua New Guinea governments shall forthwith take all steps necessary to cease and prevent the continued unconstitutional and illegal detention of the asylum seekers or transferees at the relocation centre on Manus Island and the continued breach of the asylum seekers' or transferees' constitutional and human rights."
Manus Island: a controversial 'solution'
- The camp first opened in 2001 under Prime Minister John Howard.
- It was formally closed in 2008, but reopened in 2012 when a spike in asylum seeker arrivals became a major political issue for the Labor government.
- In 2013 Australia agreed to give Papua New Guinea A$400m ($309m; £212m) in aid in exchange for housing a camp and to resettle genuine refugees.
- Deadly riots broke out in February 2014 when local residents entered the facility. In January 2015 some detainees barricaded themselves inside their compound and went on hunger strike.
- Private security contractor Broadspectrum, formerly Transfield Services, currently runs the camp.
- Australia's government says its refugee policy has led to a sharp drop in the number of migrant boats.
- UN agencies and rights groups have criticised conditions at the camp and say Australia is shirking its responsibilities towards refugees and migrants.
Policy 'won't change'
Australian Immigration Minister Peter Dutton said in a statement that the court's decision did not change Australia's immigration policy.
"People who have attempted to come illegally by boat and are now in the Manus facility will not be settled in Australia," the statement said, repeating the government's long-standing policy.
The opposition Labor party urged the government to hold urgent talks with Papua New Guinea, while opponents of hard-line immigration policy declared offshore processing of asylum seekers dead.
"The game is up. The government has got to shut the Manus Island detention camp and bring these people here," Greens Senator Sarah Hanson-Young said.
"This is an important decision and I welcome it. It shows that the Australian government has been illegally detaining refugees on Manus Island for years."
Papua New Guinea's Prime Minister Peter O'Neill said the Manus Island centre should eventually close, during an address to Australia's National Press Club in March this year.
"Who is going to pay for it? Certainly the PNG government does not have the resources to resettle the refugees," he said. "We are also reassessing the numbers who are supposed to be resettled."