With baseball caps pulled low over their eyes and scarves wrapped tightly around their mouths, the young men huddled at sundown to pay tribute to a killer.

More than a dozen had come to the scene where their “brother” was shot dead by police after he sprayed gunfire outside a cafe and a synagogue. Now they would give him a proper — and defiantly public — send-off: quiet prayers, followed by repeated chants of “Allahu Akbar” and the raised-index-finger salute of the Islamic State.

“May Allah show you grace,” read the handwritten sign they taped to the bullet-scarred apartment building where 22-year-old Omar Abdel Hamid el-Hussein fell. “Rest in peace, Captain.”

Hours later, an estimated 30,000 Danes held torches to the freezing Baltic wind in their own Monday evening commemoration — this one for Hussein’s victims. Swaying to the rhythm of John Lennon’s “Imagine,” they vowed not to bend to the radicalism that drove the gunman to take two lives over the weekend in the country’s first fatal terrorist attack in three decades.

“When violence and hatred hits Denmark, the answer is community and democracy,” Prime Minister Helle Thorning-Schmidt told the solemn crowd.

Copenhagen remains somber after a gunman killed two people in separate shooting incidents over the weekend. (Reuters)

But the earlier, far smaller gathering offered ominous hints of just how long and difficult this country’s struggle with violent extremism may prove to be.

Danish Foreign Minister Martin Lidegaard told the BBC that the weekend’s attacks were believed to be the work of a “lone wolf,” a troubled young man who had already had multiple run-ins with the law and who had not acted in concert with a broader terrorist cell.

But authorities said Hussein was part of a network — a criminal gang called the Brothas that has traditionally traded in drugs and theft but whose members have lately been lured by radical Islam.

Like Hussein, many are the sons of Middle Eastern immigrants who have struggled to find a place in Denmark. These young men turn to the bonds of gang life and religion to forge their identities.

“The Brothas is dominated by young men without an education who feel they are not accepted in society,” said Aydin Soei, author of “Angry Young Men,” a book about gangs and inner-city life in Copenhagen. “They are not good Muslims and they know it. But Islam is part of their identity. They have a group identity of everyone being against them and being underdogs. In that identity, Islam means something.”

Soei said he thinks he met Hussein in 2011 while conducting research in Norrebro, the ethnically and socioeconomically diverse north Copenhagen neighborhood where Hussein lived – and, in the hours before dawn Sunday, died.

A group of Brothas members he had run into asked Soei where he was from — and were instantly suspicious when he mentioned another Copenhagen neighborhood that is home to a rival gang.

“They were really not happy about that,” Soei said. “That tells a lot about the gang war in Copenhagen.”

Turf battles over drugs and pride have led to sporadic outbreaks of violence. But they have mostly been limited to fistfights and the occasional stabbing.

“The gangs around here would get wiped out in one day by even some mediocre gang in the United States,” said Adeel, manager of a Norrebro Internet cafe. He declined to give his last name because he did not want the attention.

The cafe was raided Sunday by police who believe Hussein may have paid a visit in between his attacks Saturday afternoon. Police arrested and charged two people who they believe may have helped Hussein by “giving advice and assistance in connection to the shooting,” a police spokesman said.

Adeel said he did not know Hussein but that gang members are occasional visitors to the Internet cafe — and often engage in petty neighborhood crime. “They’re local punks,” he said.

Lately, however, Copenhagen’s gangs have taken on a more troubling cast as the line between criminality and extremist ideology has blurred.

Copenhagen gang leaders are among at least 110 Danes who have ventured to Syria and Iraq to fight in those countries’ wars. Intelligence services have stepped up monitoring of the groups’ activities for fear of the sort of domestic terrorist attack that played out over the weekend.

Hussein had been on law enforcement’s radar for years, having graduated from small-time weapons charges to an assault conviction after stabbing a teenager on a commuter train in November 2013.

He was released from prison just two weeks before the weekend’s rampage, and Danish ­media reported Monday that prison officials had warned the country’s main security service that he might have become radicalized during his jail term. The security service, P.E.T., would not comment on that claim and has declined to say whether Hussein was under surveillance before the shootings.

His first target Saturday was a free-speech forum convened by Lars Vilks, a Swedish cartoonist who has been repeatedly targeted by radical Islamists for depicting the prophet Muhammad. His second was the Great Synagogue of Copenhagen. In each attack, one civilian was killed and police officers were credited for preventing many more deaths.

The idea of Hussein as a terrorist was one that those who knew him in Norrebro said they had trouble squaring with the quiet and calm man they had known.

At the Thai kickboxing club where Hussein worked out and competed, his trainer said he was stunned by the suggestion that his onetime protege had been behind the killings.

“He didn’t talk too much, but he was a cool guy,” said the trainer, who declined to give his name because he did not want to be associated with violence. “Women train in here. Danes train in here. He was cool with everyone.”

Hussein came from a family of Palestinians who moved to Denmark from Jordan. He spent at least part of his upbringing in Mjolnerparken, a set of state-subsidized red-brick apartment blocks.

The area is known to many Danes as a low-income ghetto, but it is packed with well-
maintained playing fields, parks and community centers — all products of a substantial government investment.

The area is also a haven for the Brothas.

After paying their respects Monday evening at the spot where Hussein was killed, the young men — many no older than their late teens — walked en masse back to Mjolnerparken. At least a dozen police carrying assault rifles followed from a wary distance.

“There’s only one terrorist, and that’s the guy who drew the prophet,” said Adnan Abdec, a 29-year-old Muslim who was not with the group but who watched its actions approvingly. “He made 1.5­ billion people angry. If you push Muslims, it’s going to come back to you.”

Across the street, gray-bearded Shaker al-Abadi could only shake his head and wonder what’s in store for the country that welcomed his family after they fled Iraq 24 years ago.

“I’m Muslim, but I can’t understand what I’m seeing,” said the 63-year-old shopkeeper. “Why is this happening? I’m afraid it won’t stop.”

Adam reported from London. Maren Mosaker in Copenhagen contributed to this report.