TUNIS — Fear engulfed Tunisia on Monday that Islamic State mayhem was spilling over from neighboring Libya, as dozens of militants stormed a Tunisian town near the border, assaulting police and military posts in what the president called an unprecedented attack.

At least 54 people were killed in the fighting in the town, Ben Gardane, which erupted at dawn and lasted for hours until the security forces chased out what remained of the assailants. An enormous stash of weapons was later found.

The authorities said at least 36 militants were among the dead. The others were a mix of security forces and civilians, including a 12-year-old girl.

It was unclear where the assailants had come from, although some witnesses reported that they had local accents and had pronounced themselves as liberators. But President Beji Caid Essebsi of Tunisia, increasingly alarmed about the Islamic State’s expansion in Libya, blamed the militant group. In a televised address, he suggested that the motive was to create a new Islamic State territory on Tunisian soil, similar to the 150-mile stretch it controls in Libya.