CAIRO — Egypt’s military killed 23 militants in the northern Sinai Peninsula early Thursday, a senior security official said, as the government sought to reassert control and eradicate what it called the area’s “terrorist dens” after the largest assault there by jihadists affiliated with the Islamic State.

That assault, on Wednesday, stunned officials in President Abdel Fattah el-Sisi’s government with its scale and audacity. Militants belonging to a group calling itself Sinai Province launched coordinated attacks on military checkpoints in the northern Sinai before storming a town and occupying it for hours. The military carried out airstrikes with F-16 warplanes on the town, Sheikh Zuwaid, to force the militants to retreat.

A military spokesman said 17 soldiers had been killed, though other estimates, including by the Egyptian news media, put the toll much higher. The attack was a blow to Mr. Sisi, coming just two days after militants assassinated Egypt’s top prosecutor with what the authorities said was a remote-controlled car bomb. Both attacks highlighted the government’s struggles against a broadening insurgency.

As funerals were held across Egypt for the dead soldiers, families in Sheikh Zuwaid looked for a way to leave the town, quiet now but filled with reminders of a terrible fight: a partly collapsed three-story building, scorched vehicles and the bodies of at least 15 militants.