The High Court ruled in a 4-3 decision that indigenous Australians cannot be deported even if they do not hold Australian citizenship.
The court had heard the case of two men who were born overseas but identified as being from indigenous tribes: Brendan Thoms and Daniel Love.
Thoms, 31, was released after 501 days in immigration detention within hours of the court ruling that his indigenous status entitled him to live in Australia, his lawyer Claire Gibbs said.
"Brendan has had 500 sleepless nights worrying he could be deported at any time, and that is now thankfully at an end," Gibbs said in a statement .
"He is very happy to have been released and to now be reunited with his family at long last," she added.
Acting Immigration Minister Alan Tudge said the Home Affairs Department would review other cases that might be affected by the precedent.
" The High Court delivered a significant judgment today which has implications for our migration programs," Tudge said in a statement.
The court had "created a new category of persons" that was neither an Australian citizen nor a non-citizen, he added.
The government attempted to deport the men on character grounds in 2018 after they served prison sentences for violent crimes. The government has been criticized for deporting some criminals who have lived in Australia since where were children but had never become citizens.
The court found that Thoms, 31, who was born in New Zealand to an indigenous Australian mother, was an Aboriginal Australian.
Thoms had lived in Australia since he was 6, is accepted as a member of the Gunggari tribe and is recognized as a native title holder of their traditional land.
But a majority of judges was not convinced that Love, 40, was indigenous and was accepted as a member of the Kamilaroi tribe.
He was born in Papua New Guinea to an indigenous Australian father and has lived in Australia since he was 5.
His lawyers say he will provide more evidence of his Aboriginality and another trial could be held to decide the issue.
Both Love and Thoms were placed in immigration detention and threatened with deportation on their release from prison after serving sentences for unrelated crimes.
Love has had his visa restored since his lawyers initiated court action in 2018 and lives on the Gold Coast.
Thoms had been in immigration detention in Brisbane for 16 months since he completed a six-month prison sentence.
The court found Aboriginal Australian have a special cultural, historic and spiritual connection to Australia which is inconsistent with them being considered "aliens" in the meaning of the Australian constitution.
Gibbs said the government had used its constitutional powers to deal with aliens "inconsistently, unfairly and, now we've proven, unlawfully."
"This case isn't about citizenship. It's about who belongs here, who's an Australian national and who's a part of the Australian community," Gibbs told reporters outside court.
"The High Court has found that Aboriginal Australians are protected from deportation. They can no longer be removed from the country that they know and that they have a very close connection with," she added.
The Home Affairs Department did not immediately respond to a request for comment.
Gibbs said both Love and Thoms would sue the government for wrongful detention.
"Both of my clients have suffered severe embarrassment about being Aboriginal men in immigration detention and they've been subject to a lot of ridicule," Gibbs said. "So it's been a very, very tough time for them both."
Mary Crock, professor of public law at Sydney University, described the decision as "radical."
She noted the case arose from Australia's increasingly tough policies of deporting non-citizens who break the law, some to countries where they don't speak the language or have any real connections.
"I see it as a very positive development," Crock told Australian Broadcasting Corp. of the ruling.
"The case only went to the High Court because Australia uniquely has been trying so hard to deport so many different people. I can't think of another country that has gone as far as we have towards locking up and then trying to deport people who literally have no other country that they deeply belong to," she added.
Indigenous Australians make up 3% of the population and are the most disadvantaged minority group in a range of measures. Indigenous Australians die younger than other Australians and are overrepresented in prisons.