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
ablogger 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.
PhotoThe 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 ImagesAs 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.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.
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.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.
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 June 9, 2016, on page A1 of the
New York edition with the headline: Bloggers, Bangladesh and Insight on 39 Killings.
Order Reprints| Today's Paper|Subscribe