A judge has called the legality of Canada's immigration-detention system into question in a case in which he set free a refugee claimant who had done nothing wrong, yet was detained in a maximum-security prison for 17 months.

Ontario Superior Court Justice Edward Morgan said the adjudicators who preside at detention hearings and review detentions on a monthly basis appear not to be truly independent from the immigration police force known as the Canada Border Services Agency. Instead, the adjudicators accept as fact whatever allegations the CBSA officers present, he said. And once they order an individual detained, they interpret their role in a such a way as to make it difficult or impossible to let that person go.

This pattern, Justice Morgan said on Monday, appears to go beyond the case of Ricardo Scotland, a 38-year-old from Barbados, whose experience he likened to that of the central figure in Franz Kafka's novel The Trial, a classic description of a justice system gone mad.

Story continues below advertisement

Related: As asylum seekers flock to Quebec, Canadian agencies pitch in

"As with Kafka's protagonist Joseph K, no one knows why he is detained," he said in his ruling.

The judge's ruling raises questions of whether Canada regularly detains refugee claimants and other non-citizens arbitrarily, in violation of the Charter of Rights and Freedoms.

In the latest fiscal year, 2016-17, Canada detained more than 6,200 immigrants or refugees, including 439 for more than 90 days. As in Mr. Scotland's case, most were deemed "unlikely to appear" at hearings or a deportation. The court ruling comes as throngs of refugee claimants, many originally from Haiti, cross the U.S. border seeking permission to stay in Canada.

Subodh Bharati, a lawyer representing Mr. Scotland, said many other detainees are being denied basic fairness. "Justin Trudeau says, 'It's 2017,' and this is what's happening in 2017 to too many people," he said in an interview.

Mr. Scotland – a single father of a 13-year-old girl, looking worn out after his longest single stretch in custody, 10 months in a provincial facility near Niagara Falls – said in an interview that he would celebrate his freedom "just by being with her. Being with her is a celebration."

He said the adjudicators "kept calling me a liar and saying, 'We can't trust you,' but I tried everything in my power to do whatever is right."

Story continues below advertisement

The CBSA had accused Mr. Scotland of four violations of the conditions under which he was permitted to live in the community while awaiting the outcome of his refugee application, but Justice Morgan said all four were false or illogical. (One was not reporting his change of address to the authorities when he was jailed on a charge that was eventually dropped.) Even so, as the false allegations mounted, the adjudicators pointed to the growing total and said Mr. Scotland could not be trusted. Justice Morgan mocked that reasoning: "That's some catch, that Catch-22," he wrote, referring to Joseph Heller's novel about a system with paradoxical rules.

Justice Morgan set Mr. Scotland free citing habeas corpus – the "most celebrated writ in the English law," he called it – under which a detainee may appear before a judge to argue his detention is unlawful. Federal lawyer Bernard Assan, representing Attorney-General Jody Wilson-Raybould, had argued that although Mr. Scotland was neither a danger nor a flight risk, he should be detained anyway, because the court lacked jurisdiction to free him.

Janet Dench, the executive director of the Canadian Council for Refugees, said that in her group's view, most detainees are held unnecessarily, and as a result of an arbitrary process. "The fundamental problem is that the law is very broad and gives enormous power to the government to detain people," she said in an interview. However, she also said that adjudicators in some regions are far less likely to order detention than in others.

Scott Bardsley, a spokesman for Public Safety Minister Ralph Goodale, said the minister's office is reviewing the decision, and is committed to "minimizing the use of immigration detention by expanding alternatives to detention." Under the Liberal government, he said, the number of detentions has decreased each year. There were more than 9,500 detentions in 2012-13.

"The IRB is a quasi-judicial, independent body, which provides a fair hearing to asylum claimants," said a statement from the office of Immigration Minister Ahmed Hussen, whose department is responsible for the Immigration Review Board. "Decisions are made based on the merits of the specific facts presented in an individual case, and in accordance with Canada's immigration laws."

Justice Morgan laid out in detail, and with seeming disbelief, the basic flaws in logic from the Immigration Division.

Story continues below advertisement

For instance, in a detention review hearing last May, in which even the CBSA argued for Mr. Scotland's release, the adjudicator (unnamed in the Morgan ruling), refused to let him go. While the latest allegations against him had been shown to be false – breaking a curfew that no longer existed, being at a car dealership without the surety who guaranteed his bail (a video showed the surety was present), being with a no-longer prohibited person – the adjudicator wrote that being cleared by the evidence was not enough. "The fact that you put yourself in the position to have concerns about with whom you were in contact … shows an issue or a concern with your lack of judgment."

In another supposed breach, Mr. Scotland had been reporting to police four times a week for a year – but did not show up one day, and police charged him with violating bail. Appearing before a judge, he explained that the August civic holiday had thrown him off schedule, he had been taking his daughter to cheer-leading practice each day, and he had confused Friday for Thursday. The judge accepted his explanation and found him not guilty. "He forgot … he was happily being dad." Even so, CBSA insisted it was a breach, and the Immigration Division adjudicator wrote that "it is within their right to find a breach if they find it to be a breach." Mr. Scotland was returned to detention.

Even in the supposed failure to report a change of address, Mr. Scotland had telephoned a 1-888 number to tell the CBSA he was in jail, and the CBSA acknowledged as much. But the CBSA insisted it was a breach anyway, and the Immigration Division accepted that view.

Scathingly, Justice Morgan wrote, "The detention review process becomes a closed circle of self-referential and circuitous logic from which there is no escape." Mr. Assan, for the federal government, had asked that if Justice Morgan freed the detainee, he ask the CBSA to set release conditions. Justice Morgan refused to do so, and set no conditions, other than that Mr. Scotland keep the peace and attend all hearings.