DHAKA, Bangladesh — The young man, inching past a crowded checkpoint near a truck stand in Bangladesh’s capital, caught the attention of an alert police officer.

His backpack, together with his appearance, from the unshaven beard to the long Punjabi tunic over baggy pants, set off the suspicion that he was an Islamist militant. The man was arrested after he was found to be carrying a machete, an unregistered pistol and six bullets.

Little was known about the attackers, except that they were Islamist radicals, and that their assaults have been coming with ever-greater frequency this year.

The detained man refused to discuss much, saying only that he was Saiful Islam, 23 years old and a teacher at a local madrasa, or Islamic school.

But the picture filled in six days later, when two 19-year-old men, arrested after running from the site of another fatal attack, identified the madrasa teacher as a fellow conspirator. That touched off a cascade of revelations that, for the first time, has allowed the Bangladeshi authorities to penetrate the murky world of the attackers and answer questions about the planning, execution and purpose of the attacks that have baffled the country — and, indeed, the world — since the violence began.

In a lengthy interview, the chief of the police counterterrorism unit, Monirul Islam, who assumed his post in February, laid out the findings of his investigation in minute detail.

The killings were organized by two militant Islamic groups that have gathered volunteers and recruits, trained them and eventually seeded them into cells run by a commander, Mr. Islam said. They have tried to pick their targets with care, with the aim of gaining support from the public, he said, and trained teams of killers. Their goal was to convert Bangladesh’s mixed secular and religious culture to an Islamist one, the chief investigator said.

The Bangladeshi authorities say that they now believe they have identified the top leadership of the two groups they say are responsible, and that they are preparing to round them up. Only when the leaders are caught, they caution, will the attacks be stopped, and at that, only for a while if the appeal of Islamic fundamentalism is not blunted.

Secular Versus Islamists

Bangladesh, a nation with a Muslim majority adjoining eastern India, gained independence from Pakistan in a vicious war in 1971 and established a secular, democratic government. A military coup in 1975 led to more than three decades of mostly military-backed governments sympathetic to Islamic fundamentalists, until a secular government returned to power in 2009 with an overwhelming majority. But secularism is far from universally accepted in Bangladesh, and has always had to contend with a conservative Islamic culture.

To a surprising extent, the militants have succeeded in their aim of discrediting secularism, the chief investigator said.

“In general, people think they have done the right thing, that it’s not unjustifiable to kill” the bloggers, gay people and other secularists, he added.

They have also put the secular government on the defensive. As a result, even as the government has condemned the killings, it has urged writers not to criticize Islam and warned that advocating “unnatural sex” is a criminal offense.

Some experts say that only a more widespread crackdown will stop the killings, but that the government has held back, fearful of creating a backlash.

“The politics has been turned into the secular versus the Islamists,” said Abdur Rashid, a retired army major general and executive director of the Institute of Conflict, Law and Development Studies in Dhaka. “Therefore the government is cautious.”

While the killers’ strikes often appear random, Mr. Islam says the terrorism campaign was conceived by the militant groups quite deliberately as a response to mass protests in early 2013, known collectively as the Shahbag movement. Inspired by a group of bloggers who led the protests, the demonstrators advocated an end to religion-based politics and the prosecution of war crimes dating to the 1971 war for independence.

Image

Mourners carrying the coffin of Ahmed Rajib Haider, a blogger, in Dhaka in February 2013.CreditPavel Rahman/Associated Press

War crimes prosecutions have been a particular source of anger for Islamists. They were shelved during the period of military-backed rule but revived under the new democratic government in 2009. Four of the five convicted and executed in the latest round of trials were leaders in the country’s largest Islamist political party, Jamaat-e-Islami, outraging Muslim fundamentalists and others.

Two groups in particular took up the fight against secularism, Mr. Islam said. One, Ansar al-Islam, is led by a fiery cleric and a charismatic, well-trained operational commander, both of whom Mr. Islam declined to identify because they are being watched. Its leaders command about 25 trained killers, some of whom have been involved in three or four attacks, Mr. Islam said.

The second, the Jama’atul Mujahedeen Bangladesh, is the reorganized offshoot of a group banned in 2005 for setting off nearly 500 bombs simultaneously around the country.

While both are radical Islamist groups, Mr. Islam said, neither seems to have direct links to larger terrorist networks like Al Qaeda and the Islamic State, though those groups have occasionally claimed credit for the attacks.

‘It Is Your Duty to Kill’

The Islamist groups appear to have reacted quickly to the Shahbag movement, mounting their first fatal attack on Feb. 15, 2013, against a blogger who wrote critically of Islam under the pseudonym Thaba Baba. It was carried out by a group of students from North South University in Dhaka, who were incited by the sermons of the spiritual leader of Ansar al-Islam at the time, a 45-year-old cleric named Jasim Uddin Rahmani.

The students used to attend his Friday speeches at a local mosque where Mr. Rahmani, who has since been arrested, declared a fatwa on bloggers critical of Islam, calling for them to be killed, Mr. Islam said.

Image

The Bangladeshi police with two suspects in the killing of Mr. Haider, a blogger and secular activist, outside a court in Dhaka in 2015.CreditMunir Uz Zaman/Agence France-Presse — Getty Images

As it happened, one of the students, a 32-year-old senior named Redwanul Azad Rana, was also a leader in Ansar al-Islam, Mr. Islam said. He invited the younger students to Mr. Rahmani’s sermons and introduced them to the writings of Thaba Baba.

“Being a believer, it is your duty to kill” Thaba Baba, Mr. Rana told the students, one of them said in his confession in court. Prodded by Mr. Rana, “we made a plan to kill Islam’s and Prophet Muhammad’s insulter Thaba Baba by identifying him,” the student, Faisal Bin Nayeem, 24, said in the statement.

He said they found Thaba Baba’s picture on Facebook, then searched for someone matching it at the Shahbag protest, still underway at the time. Eventually, they identified a 32-year-old architect, Ahmed Rajib Haider, as Thaba Baba. After studying Mr. Haider’s routines, three of them surprised him outside his house around 9 p.m. Mr. Nayeem said he drove his machete into the back of Mr. Haider’s neck and hit him twice more as he fell forward.

Ansar al-Islam, with the help of mainstream Islamist groups, then began to publicize Mr. Haider’s writings, casting the killers as defenders of Islam against “atheist bloggers.”

The writings, published in at least two national dailies, enraged large segments of the population, who had previously been sympathetic to the Shahbag movement, Mr. Islam said.

During the next two months, two more bloggers were killed. The police began arresting the North South University students who were involved in killing Mr. Haider, and also caught Mr. Rahmani, the cleric who inspired them. But Mr. Rana, the student leader, remains at large and is thought to have left the country.

Image

Followers of the Sufi leader Hazrat Dewanbagi offered Friday prayers during the annual congregation in Dhaka on June 3. Tens of thousands of Sufis descended on the Bangladeshi capital in a defiant show of unity after a spate of targeted killings by suspected Islamist militants.CreditAgence France-Presse — Getty Images

Mr. Islam said he believes these arrests stopped Ansar al-Islam — also known as the Ansarullah Bangla Team — from killing more people in 2013 and 2014. But the group reorganized the terrorist cells, he said, and the killing resumed.

In February of last year, Mr. Islam said, Ansar attackers killed Avijit Roy, 42, an American citizen of Bangladeshi origin. Mr. Roy worked by day in the biotechnology industry in the United States and by night as a writer of books on science, homosexuality and religion, in addition to founding a website called Mukto-Mona, Bengali for freethinker.

From the growing number of attackers in detention, the police learned that the newly reconstituted Ansar al-Islam had changed its tactics, now recruiting madrasa students and teachers instead of university students to carry out killings. Mr. Islam said a violent protest by the madrasa students in May 2013 convinced Ansar al-Islam leaders that they were a more promising source of fanatical recruits than their university counterparts.

The training and indoctrination of the recruits became more rigorous and systematic at that time, Mr. Islam said. The cell that assassinated another blogger, Oyasiqur Rahman Babu, 27, just a month after Mr. Roy’s killing rented an apartment where two senior operatives worked with the group of would-be killers. One, an operations expert, taught them how to kill with a machete and use a pistol to scatter anyone interrupting the attack.

Armed with Mr. Babu’s picture and his address, the assassins were sent to his home to assess the situation and returned to a barrage of questions from the trainer. “What happens if you are stopped? What will you do?” he asked them, Mr. Islam said.

Close to the planned date of the attack, the other operative, an ideologist, introduced the killers to Mr. Babu’s writings. The students were given samples calculated to stir them up. “What is the punishment for someone who writes these insults?” the trainer asked them. The group answered in unison, day after day, “Only death,” the arrested students told investigators.

Mr. Islam said the hardest part for the police was identifying the leaders, who were so concerned about security that they would not give their real names to the madrasa students they were training.

Still, the police have now identified a trainer involved in planning the attack on Mr. Babu. Last month they printed the suspect’s picture, along with those of five others accused of participating in the killings, in local dailies, offering rewards of up to 500,000 takas, about $6,400, for information leading to their arrests.

Costly Slip-Ups

The other militant group, the reorganized Jama’atul Mujahedeen Bangladesh, works independently of Ansar al-Islam and almost exclusively in northern Bangladesh, the chief investigator said. But the group is less professional than Ansar al-Islam, he said, making mistakes that are costing it public support.

The group has trained 50 to 100 madrasa students as killers, he said, organizing them into cells of four or five. But through shoddy research, many of the victims have turned out to be popular local figures. Among them: a homeopathic doctor who used to give free treatment to villagers, and an English professor at Rajshahi University who was not known to have written critically of Islam.

When detained militants learned that a 66-year-old Japanese man they had slaughtered had converted to Islam in 2015, they told investigators they were upset over their mistake.

With all the slip-ups, the communities turned against them. With the public’s support, Mr. Islam said, the police quickly rounded up the suspected hit men and several of their handlers in most of the Jama’atul Mujahedeen Bangladesh killings, and were in pursuit of the senior leadership.

Many in Bangladesh continue to live in terror. Twenty-five associates of one victim, a gay rights activist, have taken refuge in safe houses provided by diplomatic missions. Several dozen bloggers have fled the country. Those who remain have grown fatalistic.

“On this journey, we’ll lose our lives,” Arif Jebtik, 39, one of the leaders of the Shahbag movement, said in an interview in his Dhaka apartment, which he rarely leaves. He has quit his job, closed his blog and stopped dropping his children off at school.

“This is the price we have to pay to history,” he said.

A version of this article appears in print on , Section A, Page 1 of the New York edition with the headline: Bloggers, Bangladesh and Insight on 39 Killings. Order Reprints | Today’s Paper | Subscribe