CAIRO — In the deadliest attack on civilians in Egypt’s modern history, Islamist militants detonated a bomb inside a crowded mosque on Friday and then sprayed gunfire on panicked worshipers as they fled the building, killing at least 235 people and wounding at least 109 others.

The scale and ruthlessness of the assault, which occurred in a small town in the insurgency-racked Sinai Peninsula, sent shock waves across the nation, not just for the number of deaths but also for the choice of target. Attacks on mosques are rare in Egypt, where the Islamic State has targeted Coptic Christian churches and pilgrims but avoided Muslim places of worship.

The attack injected a virulent new element into Egypt’s militant mix because most of the victims were Sufi Muslims, who practice a mystical form of Islam that some extremists deem heretical. And it underscored the failure of President Abdel Fattah el-Sisi, who has insisted he needs to crush political opposition to combat the threat of Islamist militancy, to deliver on his promises of security to ordinary Egyptians.

“We will respond to the terrorist attack in Al Rawda,” Mr. Sisi vowed in a televised speech. “The military and the police will take revenge.”