The Danish-Arab gunman who attacked a free-speech seminar and a synagogue in Copenhagen was released about two weeks ago from a jail where he may have been radicalized while serving time for a vicious stabbing.

As Denmark mourned the two victims, these and other troubling details emerged Monday about Omar Abdel Hamid El-Hussein’s path to the country’s worst terror spree in three decades.

El-Hussein was arrested 15 months ago in a vicious knife attack on a train passenger, and while he was awaiting trial, a change in his behavior last summer set off enough “alarm bells” for jail authorities to alert PET, Denmark’s counter-terror agency, a person close to the investigation said.

Such warnings usually set in motion counter-radicalization efforts, such as counseling in jail. It wasn’t immediately clear whether the court was aware of the issue before El-Hussein was convicted of a lesser charge.

Sentenced to the time he had already served, he was released about two weeks ago, the person with knowledge of the case said, speaking on condition of anonymity because police haven’t officially identified the gunman.

“We are working on finding out what has happened,” PET spokeswoman Lotte Holmstrup said Monday.

The agency’s director, Jens Madsen, also wouldn’t elaborate, but he confirmed Sunday that PET had been aware of the gunman, and that El-Hussein may have been inspired by last month’s attacks by Islamic extremists in Paris that killed 17 people.

The 22-year-old opened fire at a cultural center and a synagogue — targets that resembled those in the Paris attackers’ rampage at the Charlie Hebdo newspaper and a Jewish grocery store — before he was killed in a gun battle with a SWAT team early Sunday.

The shootings left a Danish documentary filmmaker and a Jewish security guard dead. Five police officers were wounded.

Denmark’s prime minister and crown prince and foreign dignitaries joined about 30,000 people honoring the victims in the bitter Monday evening cold outside the Krudttoenden cultural center, whose name in Danish translates to “powder keg.”

“I am here with my daughter to show her that we live in a free country. No one must ruin it,” said Aisha Abdi, a Somali Muslim and political refugee who brought her 12-year-old daughter, Irina.

Also Monday, a judge ordered 10 days of pre-trial detention for two people accused of helping el-Hussein get rid of a weapon while evading authorities. Both men denied the charges, said Michael Juul Eriksen, a defense lawyer for one of the two.

Many Danes first saw El-Hussein’s image in November 2013, when he was arrested for gravely wounding a 19-year-old student with a large knife.

El-Hussein didn’t come across as religious, and had the appearance of a “hardened criminal,” his shaved head pocked by scars, said Jesper Braarud Larsen, a Danish court reporter who covered the December trial.

Surveillance video of the commuter train attack was so graphic that the victim’s sobbing parents had to leave the courtroom, but El-Hussein didn’t even flinch, Larsen recalled.

El-Hussein told the court he had smoked hashish and was feeling paranoid when he randomly attacked the student. Prosecutors charged him with attempted homicide but a judge convicted him of aggravated assault, taking into account El-Hussein’s claim that he never meant to kill the victim, said Larsen, who works for the Danish news site Dagens.dk.

Denmark has foiled a series of terror plots since the 2005 publication of 12 caricatures of the Prophet Muhammad in the Jyllands-Posten newspaper triggered riots in Muslim countries and calls for vengeance.

Swedish artist Lars Vilks, who caricatured the prophet in 2007, was at Saturday’s free speech event. Whisked away unharmed by his bodyguards, Vilks later said he thought he was the intended target.