AMMAN, Jordan — A prominent and outspoken Jordanian writer on Sunday was shot dead in front of the courthouse where he had been on trial for posting a cartoon deemed offensive to Islam on social media.

A Jordanian security official said the shooter was a former imam, or prayer leader, at a local mosque, and said the man had been motivated by his anger over the cartoon posted to Facebook by writer Nahed Hattar. The shooting was the latest in a string of deadly security lapses in Jordan.

Witnesses and police said Hattar, 56, was preparing to enter the courthouse for a hearing when the gunman shot him at close range.

“He was standing at a short distance of about one meter (yard) in front of Nahed on the stairs of the Supreme Court,” a witness told The Associated Press on condition of anonymity, fearing repercussions. The official Petra News Agency said Hattar was shot three times.

The witness said the shooter, who was immediately arrested, was wearing a long grey robe and long beard characteristic of conservative Muslims.

Jordanian media, citing anonymous officials, identified the shooter as Riad Abdullah, 49, a former imam in northern Hashmi, a poor neighborhood in Amman. The reports said Abdullah had recently returned from a trip abroad, but gave no further details.

The security official declined to confirm the suspect’s name. But he said he had confessed to the shooting and claimed he had acted alone and had no connections to any militant group.

Prosecutors charged the man with premeditated murder, committing a deadly terrorist act and possession of an unlicensed weapon. The suspect was detained for 15 days while the case was referred to the State Security Court.

The official, speaking on condition of anonymity because he was not authorized to speak to the media, said the suspect said he was motivated by the cartoon, which depicted a bearded man, smoking and in bed with two women, asking God to bring him wine and cashews. All physical depictions of God or the Prophet Muhammad, even respectful ones, are forbidden under mainstream Islamic tradition.

Government spokesman Mohammad Momani condemned the killing as a “heinous crime.”

“The government will strike with an iron hand all those who exploit this crime to broadcast speeches of hatred to our community,” he told the Petra agency.

But supporters of Hattar said they held the government responsible for the shooting, accusing Prime Minister Hani al-Mulki of creating a hostile atmosphere that encouraged violence against the writer.

“The prime minister was the first one who incited against Nahed when he ordered his arrest and put him on trial for sharing the cartoon, and that ignited the public against him and led to his killing,” said Saad Hattar, a cousin of the writer.

Hattar has long been a controversial figure in Jordan. Years ago, he claimed that the late King Hussein had arrested and tortured him many times for his critical writings and vowed not to mourn the king, who died in 1999.

While born a Christian, he considered himself an atheist. He was a strong supporter of Syrian President Bashar Assad and an outspoken critic of the Islamic State group and al-Qaida.

Hattar was detained in August after sharing the cartoon on Facebook. Relatives said the cartoon was meant to illustrate what Hattar viewed as the twisted religious views of Islamic State extremists.

The post was quickly deleted after many angry responses. Jordan is an overwhelmingly Muslim and deeply conservative society.

Hattar was briefly detained for two weeks before he was released on bail.

In a statement, the family called on the government to hold accountable all those who had incited against Hattar.

“Many fanatics wrote on social media calling for his killing and lynching, and the government did nothing against them,” they said.

Jordan is a close Western ally and has been largely spared from the violence engulfing neighboring Syria and Iraq. But a series of recent attacks has raised concern about security in the kingdom.

Late last year, a Jordanian police captain opened fire on instructors at an international police training center in Jordan’s capital, killing at least five people, including two Americans, before being shot dead by security forces. In June, a suicide car bomb attack near the Syrian border killed seven Jordanian soldiers.

Hundreds of Jordanians have been sentenced to prison, are awaiting trial or are being held for questioning about links to IS. Under toughened anti-terror laws, even liking or sharing the group’s propaganda on social media can land someone a prison sentence.

But on Sunday, social media accounts of prominent Islamists in Jordan and elsewhere were celebrating Hattar’s death, saying he deserved it for blasphemy.

Anja Wehler-Schoek, resident director of the Friedrich Ebert Foundation in Jordan, said she found the social media posts “shocking.” The German foundation promotes democracy and political education in the region.

“This is clearly a very dark day for Jordan, which has long been celebrated as a model of peaceful co-existence,” she said. “I am very worried we are seeing the end of an era here and more and more problems to come in the future.”

Daraghmeh reported from Ramallah, West Bank.