A migrant accused of terrorism over a deadly attack at Tunisian museum has avoided extradition to the North African country and has been released from detention for lack of evidence by an Italian court. Abdel Majid Touil came to symbolise European fears that jihadi militants could have exploited the refugee crisis to reach the West undetected, when he was arrested near Milan in May.

The 22-year-old had arrived in Italy on a migrant boat and was held on the request of Tunisian authorities over his alleged involvement in the killing of 22 people at the Bardo museum in March. Now a Milan court has ruled against his extradition due to human rights concerns, as he risks being executed in Tunisia and Rome opposes capital punishment.

"The most serious crimes the accused faces are punished by death according to the Tunisian penal code," said Giovanni Canzio, the president of the Milan court of appeals. "Capital punishment impedes extradition because it is not foreseen by Italian law."

In addition, Italian prosecutor who had opened a separate and independent investigation into the Bardo attack, which listed Italian nationals among the victims, have asked it be dropped for lack of evidence. Investigators said the probe revealed no elements linking Touil to the shooting. He is due to be released from the Milan jail in which he has been detained for five months.

Touil was arrested in the small town of Gaggiano in May. Tunisian authorities initially identified him as one of the key figures in the 18 March attack in the capital, but later downplayed his role, accusing him of having provided logistic support to the gunmen.

The Moroccan man always claimed his innocence and doubts about his supposed role were quick to arise. It soon became clear that he had arrived in the Sicilian port of Porto Empedocle aboard a boat from Libya in mid-February, a month prior to the shooting.

Upon disembarking he was identified, photographed, fingerprinted and issued with an expulsion order, not because he was considered a security threat but because Rome doesn't grant Moroccans immediate right of asylum.

He ignored the loosely enforced instruction and reached his mother and siblings living in Gaggiano. Authorities confirmed he was attending class at literacy school when armed men stormed the museum in Tunis, while family members described him as a young man who prefers local bars to the mosque.

His case was widely used by Nationalist parties across the EU, including Britain's Ukip, to fuel anti-migrant sentiments, as it was cited as evidence that Islamist fighters were infiltrating migrant boats.