A Pakistani man who was on trial for blasphemy after he claimed to be a prophet was shot dead Wednesday in a courtroom in the northern city of Peshawar.

Tahir Ahmad Naseem was killed by a man who shouted he was an "enemy of Islam" before opening fire.

Blasphemy is legally punishable by death in Pakistan, though no one has been executed for it. However, accusations of blasphemy have often led to violent attacks.

PAKISTAN PRIME MINISTER SAYS US 'MARTYRED' USAMA BIN LADEN

Police officers gather at an entry gate of district court following the killing of Tahir Ahmad Naseem, who was in court accused of insulting Islam, in Peshawar, Pakistan, Wednesday, July 29, 2020. (AP Photo/Muhammad Sajjad)

Video shared on social media of the grisly scene shows Naseem's body slumped over the court's seats.

The person who killed Naseem has only been identified as Khalid. It was not clear how Khalid brought a loaded gun into the court.

Naseem was first accused of blasphemy by a teenager named Awais Malik.

Naseem and Malik struck up a friendship online while Naseem was in the United States. Malik told the BBC that he and Naseem had met in person at a shopping mall in Pakistan to discuss his views on religion. The conversation was alarming enough for Malik to go to the authorities and open a case against Naseem.

Naseem was born into the Ahmadi sect but had left it and then claimed to be a prophet himself.

CLICK HERE FOR THE FOX NEWS APP

The Ahmadis are a persecuted minority in Pakistan and have been victims of the country's draconian laws. There are about half a million Ahmadis in Pakistan, according to a study by Human Rights Watch.

The Ahmadi community leader suggested to the BBC that Naseem had been mentally ill and had uploaded videos on social media claiming to be a messiah.