He had been facing charges for allegedly claiming to be a prophet.
Blasphemy is legally punishable by death. No-one has been executed for it by the state but accusations can often lead to violent attacks.
The victim, Tahir Ahmad Naseem, was accused of blasphemy in 2018 by a teenager.
He was killed at a trial hearing on Wednesday morning. Video shared on social media shows his body slumped over the court's seats.
His attacker was arrested at the scene. Another video shows him in handcuffs, shouting angrily that his victim was an "enemy of Islam".
Mr Naseem was first accused of blasphemy by Awais Malik, a madrassa student from Peshawar. Mr Naseem had struck up an online conversation with him whilst living in the United States.
Mr Malik told the BBC he had then met Mr Naseem in a shopping mall in Peshawar to discuss his views on religion, after which he filed a case against him with the police.
He said he had not been present at court, and had no knowledge of the shooting. The suspect arrested for the killing has been named as Khalid. It is not clear how he managed to bring a weapon into the court premises.