OTTAWA — The government of Canada on Friday formally apologized to Omar Khadr, the only Canadian imprisoned at the United States military base at Guantánamo Bay, Cuba. It also said that it had paid compensation to Mr. Khadr, a former child soldier, for violating his rights under Canadian law.

“On behalf of the government of Canada, we wish to apologize to Mr. Khadr for any role Canadian officials played in relation to his ordeal abroad and any resulting harm,” the government said in its apology. “We hope that this expression, and the negotiated settlement, will assist him in his efforts to begin a new and hopeful chapter in his life.”

When he was captured by American troops in Afghanistan in 2002, Mr. Khadr, 15 years old at the time, was severely wounded. Later, at a military commission, he pleaded guilty to using a hand grenade to kill a member of the United States military during a battle. But Mr. Khadr and his lawyers subsequently said that he had made his plea to avoid being detained indefinitely.

A Liberal Party government was in office at the time of Mr. Khadr’s capture, but the subsequent Conservative government led by Stephen Harper, who was then prime minister, repeatedly characterized Mr. Khadr as a terrorist and made little effort to secure his release.