"People outside began running for cover and shouting, 'There are men with guns!' " said Abdul Qayyum, a neighborhood 9.
Once inside, the gunmen began firing wildly and hurling grenades, witnesses said. Two of the attackers detonated suicide vests.
"The bullets seemed to be coming from all sides," said worshiper Abdul Rauf. "Everyone was panicking, racing into bathrooms or up the stairs to find a place to hide. There were dead bodies and blood everywhere."
Two of the gunmen fired at police from a minaret. One later descended and detonated his suicide, said Khusro Pervez, Lahore's top administrative official.
After a three-hour standoff, police regained control of the mosque and arrested two of the militants.
In addition to the three attackers who blew themselves up, one was killed by security forces, said Shuja Khanzada, an advisor to Punjab provincial Chief Minister Shahbaz Sharif.
Authorities said 52 people were killed and 58 injured in the Garhi Shahu attack.
Speaking to reporters in Lahore, Sharif, brother of PML-N party chief Nawaz Sharif, condemned the attacks.
"I believe this brutal act has reminded the nation once again that Pakistan can never achieve its desired goals until this kind of extremism is eliminated," Sharif said.
A special correspondent in Lahore contributed to this report.