A Muslim aimed a semi-automatic gun at a Crawley religious leader and pulled the trigger only for the weapon to jam, a court heard.
Amir Khan, 27, who was said to have fired a shot into the air outside a mosque in the town just moments before, was bundled to the ground.
Chichester Crown Court heard yesterday only the pistol jamming stopped anyone being hurt in the incident in June last year.
The jury was told Khan had been attacked before the firearm incident and the beating revolved around allegations a married woman in the local Muslim community had been having an affair, a crime punishable by stoning in Pakistan.
Irshad Sheikh, prosecuting, said Sussex Police stationed officers outside the mosque in Pelham Road during the prayer meeting on June 28 following an incident earlier in the day involving Muslims at a local school.
Towards the end of the 45-minute meeting, worshippers heard a fracas in the hall.
Ikhlaq Rafiq, who was about to read a prayer, left to see what was going on. He saw three men beating another. One was using a belt.
With help, Mr Rafiq managed to push those involved into a car park.
Mr Rafiq saw Khan pull the gun from his trousers and aim it at the group who had been attacking him.
He also said the gun was fired into the air.
The move sparked fury among a large group of worshippers who had gathered.
An elder, Younis Khan, stood in front of the defendant and pleaded with him to put the gun down.
Mr Sheikh said: "The defendant told him to get out of the way. Mr Younis did not move but, in spite of this, the defendant was seen to pull the trigger.
"By good fortune the gun jammed.
"The defendant made a desperate attempt to cock the barrel of the gun to free the mechanism. This gave Mr Rafiq and others the opportunity to pounce on the defendant."
Police intervened and arrested Khan. A ballistics expert said the gun had been modified to fire ball bearings.
In a police interview, Khan denied he had taken the gun into the mosque. He claimed the weapon was produced by another man and he prised it from him. He also alleged there were other guns at the scene.
Mr Sheikh said: "We say there was no other gun. He had produced that gun and was going to use it. He aimed it at chest height at the group who had attacked him and his intention was to endanger life. He was acting out of revenge because he had been assaulted."
Khan, of Ladypool Road, Birmingham, denies possessing a firearm with intent to endanger life and possessing a firearm with intent to cause fear.
The trial continues.
Comments: Our rules
We want our comments to be a lively and valuable part of our community - a place where readers can debate and engage with the most important local issues. The ability to comment on our stories is a privilege, not a right, however, and that privilege may be withdrawn if it is abused or misused.
Please report any comments that break our rules.
Read the rules hereComments are closed on this article