Canada's Federal Court of Appeal has upheld a lower court ruling ordering the government to seek the return a Canadian detainee at Guantanamo Bay.
The judges upheld the view of Omar Khadr's lawyers that the government's refusal to ask for his repatriation infringed his constitutional rights.
A foreign ministry spokeswoman said it would review the court's decision.
Mr Khadr, who has been in detention for six years, is accused of killing a US soldier in Afghanistan when he was 15.
Now 22, he faces multiple terrorism-related charges, the most serious of which is murder. He could be sentenced to life in jail if convicted.
In April, a Canadian federal judge ruled that Ottawa had to help Mr Khadr, a Canadian citizen born in Toronto, on the grounds that his rights under the Charter of Rights and Freedoms had been infringed.
Mr Khadr was entitled to such rights because Canadian officials had allegedly been complicit in his mistreatment, the judge said.
To mitigate the effect of the violations, Canada had to present a request for Mr Khadr's repatriation as soon as practicable, he added.
The government subsequently appealed, saying it should have "unfettered discretion to decide whether and when to request the return of a Canadian citizen detained in a foreign country".
It was "a matter within its exclusive authority to conduct foreign affairs," government lawyers said in court documents.
But the appeal court found there was no factual basis to conclude that the ruling represented a serious intrusion into the government's responsibility for foreign affairs, or that requesting Mr Khadr's return would risk damaging Canada's relations with the US.
"While Canada may have preferred to stand by and let the proceedings against Mr Khadr in the United States run their course, the violation of his Charter rights by Canadian officials has removed that option," Justice John Evans and Justice K Sharlow said.
A spokeswoman for Foreign Minister Lawrence Cannon said the government would review "carefully" the court of appeal's decision. It could still bring its case to the country's Supreme Court.
After the ruling, Mr Khadr's lawyer Dennis Edney said: "It's about time for Mr. Harper to follow the rule of law and now make real efforts to bring Omar Khadr home and not delay the process any further."
Mr Edney has said his client would be willing to be prosecuted in Canada and to undergo a period away from his family, who in the past have had ties to al-Qaida.