RABAT (Reuters) - Three Moroccan men admitted on Thursday to the killing of two Scandinavian backpackers in a grisly murder last December, saying they had committed the crime after attempting and failing to join the Islamic State.

The bodies of Louisa Vesterager Jespersen, 24, from Denmark, and Maren Ueland, 28, from Norway were found on Dec. 17 near the village of Imlil, a hiking destination in the Atlas Mountains near Marrakech.

The three men pledged allegiance to the Islamic State in a video made three days before the murder. Authorities described them after they were arrested as “lone wolves” who had not coordinated the killings with Islamic State.

However, authorities have arrested a further 21 people who they say have links to the three and face charges including forming a criminal gang to commit terrorist acts, encouraging terrorism, and undermining public order.

In a court hearing in Sale near Rabat, Abdessamad Ejjoud and Youness Ouziyad admitted to attacking and decapitating the two women, who were staying overnight in a tent in the Atlas mountains. A third suspect, Rachid Afatti, said he filmed the slaughter. Ejjoud said the video of the murder was shared with other Islamic State sympathizers.

“After failing to join the Islamic State, we decided to do Jihad at home,” said Ejjoud, a 33-year-old carpenter from Marrakech and father of two, who had previously spent two years in jail on terrorism-related charges.

“I regret what happened and I am still trying to grasp it,” he said.

The three were arrested at a bus station in Marrakech four days after the murder.

They said they were heading south towards the city of Laayoune but the prosecutor said they were attempting to join Jihadi groups in the Sahel via the Algerian border.

The lawyer representing the family of Vesterager Jespersen said the state should pay compensation to her family. Some of those arrested had previously been jailed on terrorism charges or were known to have attended schools preaching radical ideas, the lawyer argued. The family of Ueland did not hire a lawyer.

State lawyer Abdellatif Ouahbi called the crime “an isolated regrettable act” and said in a statement to Reuters that Morocco was “a model in counter-terrorism.”

Some 1,700 Moroccans have joined the Islamic State militant group in the Middle East, according to Moroccan officials.

Compared with other countries in North Africa, Morocco - a popular travel destination for Europeans - has been largely insulated from militant attacks. The most recent took place in April 2011, when 17 people were killed in the bombing of a restaurant in Marrakech.

In 2017 and 2018, Moroccan authorities said they dismantled 20 militant cells planning attacks in the country.