A gunman shot dead Jordanian writer Nahed Hattar on Sunday outside the court where he was to stand trial on charges of contempt of religion after sharing on social media a caricature seen as insulting Islam, witnesses and state media said.

The gunman was arrested at the scene, state news agency Petra said. A security source said he was a 39-year-old Muslim preacher in a mosque in the capital.

Hattar, a Christian and an anti-Islamist activist, was arrested last month after sharing on social media a caricature depicting a bearded man in heaven smoking in bed with women and asking God to bring him wine and clear his dishes.

Lebanon's powerful Shi'ite Hezbollah militant group mourned Hattar as a "brave and vocal voice" against the Takfiris, a derogatory term used to describe hard-line Sunni fundamentalists such as Islamic State.

Many of Jordan's conservative Muslims considered Hattar's move deeply offensive. Still, such politically motivated assassinations are rare in the U.S.-backed Arab kingdom, whose relative stability has distinguished it from war-ravaged neighbours such as Syria and Iraq.

Hattar, who was also Jordan's most vocal supporter of Syrian President Bashar al-Assad, was charged with contempt of religion and sowing sectarian tensions. The country's highest official religious fatwa authority criticised Hattar for what it said was the "insult to the divine entity, Islam and religious symbols".

"The assailant was arrested and investigations are ongoing," Petra quoted a security source as saying, adding that the killer, who held a degree in civil engineering, had fired three shots at Hattar.

Two witnesses said the gunman was wearing a traditional Arab dishashada, worn by ultra conservative Sunni Salafis who adhere to a puritanical version of Islam and shun Western lifestyles.

"This killer drew his weapon from his briefcase, or the bag he was holding, and shot Nahed Hattar's chest, causing Hattar to fall to the ground. He then shot at him again, a second, third..," said Mohammad Jughbeir, who was standing close to Hattar at the time of the attack.

While many Jordanians thought Hattar had crossed a red line with the caricature, some of his secular and liberal supporters said his arrest was a breach of freedom of speech.

"Hattar's killing is a direct result of lack of commitment to freedom of expression by Jordanian authorities," said a statement from Sherif Mansour, Middle East and North Africa coordinator for New-York based Committee to Protect Journalists.

Hattar had apologised on social media and said he did not mean to insult God but had shared the cartoon to mock fundamentalist Sunni radicals and what he said was their vision of God and heaven. He had accused his Islamist opponents of using the cartoon to settle scores with him.

Hattar was a controversial figure whose calls for depriving Jordanians of Palestinian descent their political rights had won him many enemies in a country with a large Palestinian population.

The Jordanian government condemned the attack.

"The law will be strictly enforced on the culprit who did this criminal act and will hit with an iron fist anyone who tries to harm state of law," government spokesman Mohammad Momani said.

The moderate Islamist Muslim Brotherhood group also warned against a flare up in religious and sectarian tensions in a country where Christians are a minority but wield wide political and economic influence.