A prominent Jordanian writer has been shot dead in front of the courthouse where he was on trial for sharing a cartoon deemed offensive to Islam.

Police and relatives say Nahed Hattar was gunned down shortly before arriving for a court hearing. Police said the killer was immediately arrested.

Hattar was arrested in August after sharing the cartoon on social media. The drawing of unclear origin depicts God in paradise, being treated as a servant by an Arab man lounging in bed with two women and asking for wine.

Relatives said the cartoon was meant to illustrate what Hattar, a Christian, 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 country.