Dya Hammadi went to school with the man who kidnapped him. It happened in broad daylight, on the terrace of a cafe in Regueb, near Sidi Bouzid, the Tunisian town where the Arab spring revolution kicked off five years ago. The kidnapper, a man named Bassem bin Hassin, told Dya he had to stop rapping and dressing like a westerner. He took him first to the local barbershop, where a 12-year-old-boy who had been radicalised by Bassem and his friends forcibly shaved Dya’s dreadlocks off, leaving painful cuts all over his head. Then Bassem took Dya to a mosque, where a preacher read sections of the Qur’an and told him he had to join “the true path of Islam”. When Dya finally escaped, he went home, packed a bag, and took a taxi to Tunis, the capital, where he’s lived ever since.

Dya started out rapping about the injustices of the Zine al-Abidine Ben Ali regime in 2010 and rose to prominence in the underground rap scene. Now he’s one of many rappers who speaks out against the Islamic State, the Islamist Ennahda party and the growing influence of Salafism in Tunisia. We meet in a small, dimly lit cafe in downtown Tunis. The owner, who seems to know him well, refuses to accept money for the americano he orders. “This is the cafe of the activists,” Dya says, smiling. “Everyone who comes here is a friend from the days of the revolution.”

Dya has seen the growth of Salafism in the country since the fall of the Ben Ali dictatorship in 2011 and uses his music to point out its hypocrisy. In a track called Nes2el (I Ask), he sings: “You’ve learned the Qur’an by heart / But you never practised what it says / You pretend to act like the prophet / But you steal money from orphans.”

Dya lights a cigarette and waves to friends at another table. “Regueb is fertile ground for Isis because of poverty and misery,” he says. At the end of 2014, his kidnapper, Bassem, left Regueb to join Isis in Syria. About a month ago, Dya heard he had died there. However, he still fears other Salafists in Regueb will target him if he returns. “They say we wear the horns of the devil because we make music. We are targeted every moment.” Using the Arabic word for Isis, he says: “Tomorrow, if Daesh comes in, I will die. I’ll be the first to be killed.”

Many Salafists from Regueb were schoolfriends of his who had been radicalised in jail. “They are victims first, then they become part of Daesh. They’re not like how they were when we were kids. They see you as an infidel. They have to kill you.”

Terrorism is my enemy. A rapper who doesn't defend his people is not a rapper

Rap music in Tunisia is often extremely political and many rap artists became cultural icons during the 2011 revolution. Their music spoke to a generation fed up with a corrupt dictatorship, police oppression and economic stagnation. Now, almost five years later, rappers are using their music to sway Tunisia’s youth away from Islamism. For many who have lost friends and family to Isis, this lyrical combat is incredibly personal.

Mehdi “DJ Costa” Akkari started using music in this way after his younger brother Youssef left for Syria in 2012. “He changed his look,” he says, showing me before and after pictures. “He stopped talking to [his family] and stayed in his room all day, listening to verses from the Qur’an. I couldn’t do anything. I was an artist – and for a Salafist, that’s not a good thing. We stopped talking as he considered me to be a kafir.” When DJ Costa found out that Youssef was planning to leave Tunisia, he hid his passport. But his brother found a way of getting a new one, to travel to Syria.

Last year, Youssef’s Syrian wife phoned his mother to tell her he had died. “She’s still in shock,” says DJ Costa, who described the family’s grief in a song called Allah Ysaberni (meaning “God give us strength”). The lyrics reveal the depth of his feelings: “If only you could see the tears of your mother and your friends / God pity him, the one who died … God don’t spare those who did this to him.”

His brother’s transformation prompted DJ Costa to focus his music on Isis to counter propaganda targeted at impressionable young audiences. “For me, the war against terrorism is not an armed conflict but a cultural war,” he says. “Terrorism is my enemy. And a rapper who doesn’t defend his people is not a rapper.”

DJ Costa’s music found thousands of fans but put his life in danger. “I was attacked many times,” he says. Once, two men who he believes were Salafists arrived on a motorbike and tried to stab him. When I ask if he feels afraid, he instantly replies: “Not at all.” His work has always been risky, he says. “I was one of the first rappers to speak out against the Ben Ali regime before the revolution.”

Allah Jsaberni by DJ Costa, featuring a shot of his late brother, Youssef

In Lavage de Cerveau, or Brainwashing, the rapper sings: “They took you from your people and brainwashed you / They prey on your heart and your sentiments / They say come support the brothers who are being tortured / Every time death is near you, you’re a step closer to paradise.”

The authorities, still controlled largely by the dictator’s old allies, have repeatedly tried to silence him. “After my brother’s death,” he says, “I couldn’t do rap concerts any more because the state considers me a terrorist as well.” Many rappers, artists and activists have experienced similar state oppression: the Tunisian police have been criticised by human rights organisations for using the country’s harsh marijuana laws to restrict free speech.

Emino, a rapper who found fame during the revolution, was arrested for possession. He was radicalised in Tunisia’s jails, which were full of political prisoners from the days of Ben Ali. When he was released from prison, Emino pledged his allegiance to Abu Bakr al-Baghdadi, the leader of Isis. DJ Costa said he used to be one of Emino’s closest friends and the pair even tried to start a rap school, which was closed down because the government considered it a danger. “Emino was a victim of our state,” DJ Costa. “He was a young talent, who suddenly found himself in prison, surrounded by Salafists. They brainwashed him.”

Back in the dimly lit Tunis cafe, Dya talks about his own kidnappers, wondering how they could take such different paths, despite growing up in the same town, being taught by the same teachers. “At a material level, we were the same,” he says. “But I tried to learn and change things. They used to laugh at me because I would read and write poetry.”

When Dya saw young people turning towards Salafism, he started a club called Sa’aaliq, which roughly translates as The Punks. “We’d play guitars, do photography, get kids along to watch films, have theatre performances. I used my frustration to write music and make art while theirs became a virus. I chose to fight back with art.”

Like DJ Costa, Dya knows the risks of being an activist rapper. “I’m not going to make myself an easy target,” he says. “But I’ll stay a musician. I’ll remain a thorn in Daesh’s side.”

• This article was amended on 19 January 2016. An earlier version said that Emino was radicalised in Tunisia’s jails “which are full of political prisoners from the days of Ben Ali, many of them members of the right-wing Ennahda party”. This has been corrected because all political prisoners were released under an amnesty on 19 February 2011.