Tunis/Algiers: The signal to attack came from the religious place, sending dozens of militants storming through the Tunisian town of Ben Guerdan to hit army and police posts in street battles that lit the dawn sky with tracer bullets.
Militants used a megaphone to chant slogans and reassure residents they were IS, there to save the town near the Libyan border from the "tyrant" army. Most were Tunisians themselves, with local accents, and even some familiar faces, officials and witnesses to Monday's attack said.
Hours later, 36 militants were dead, along with 12 soldiers and seven civilians, in an assault authorities described as an attempt by IS to carve out terrain in Tunisia.
Whether IS aimed to hold territory as they have in Iraq, Syria and Libya, or intended only to dent Tunisia's already battered security, is unclear and the group has yet to officially claim the attack.
But as fuller details of the Ben Guerdan fighting emerge, the incident highlights the risk Tunisia faces from home-grown militants drawn to Iraq, Syria and Libya, and who have threatened to bring their war back home.
Despite Tunisian forces' preparations to confront returning fighters, and their defeat of militants in Ben Guerdan, Monday's assault shows how the country is vulnerable to violence spilling over from Libya as IS expands there.
Authorities are still investigating the Ben Guerdan attack. But most of the militants appear to have been already in the town, with a few brought in from Libya. Arms caches were deposited around the city before the assault.
"Most of them were from Ben Guerdan, we know their faces. They knew where to find the house of the counter-terrorist police chief," one witness, Sabri Ben Saleh, told Reuters. "They were driving round in a car filled with weapons, my neighbours said they knew some of them."
Troops have killed 14 more militants around Ben Guerdan since Monday. Others have been arrested and more weapons seized.
Officials say they are still determining if the militants had been in Libya before or had returned from fighting with IS overseas. But that such a large number of militants and arms were in Tunisia is no surprise.
After its protests in 2011 to topple Zine El Abidine Ben Ali, Tunisia has struggled with growing militancy.
More than 3,000 Tunisians have left to fight with IS in Iraq and Syria, according to government estimates. Tunisian security sources say many are with IS in Libya.
Gunmen trained in Libya were blamed for attacks on tourists at the Bardo Museum in Tunis a year ago and at a beach hotel in Sousse in June.
Tunisians also play a major role in IS in Libya where they run training camps, according to Tunisian security sources.
But the scale of Monday's attack was unprecedented. The militants were well-organised, handing out weapons to their fighters from a vehicle moving through the city, with knowledge of the town and its military barracks.
"We came across a group of terrorists with their Kalashnikovs, and they told us: 'Don't worry we are not here to target you. We are the IS and we are here for the tyrants in the army,'" said Hassein Taba, a local resident.
The attack tests Tunisia at a difficult time. After IS violence last year, the tourism industry that represents 7 per cent of the economy is struggling to tempt visitors to return.
With its new constitution, free elections and secular history, Tunisia is a target for militants looking to upset a young democracy just five years after the overthrow of leader Ben Ali.
"The battle of Ben Guerdane in Tunisia, 20 miles from the Libyan border... is proof enough that the IS has cells far and wide," said Geoff Porter, at North Africa Risk Consulting. "But what these cells can reliably do... and how they are directed by IS leadership in Sirte, let alone in Iraq and Syria, is not known."
IS has grown in Libya over the past year and half, coopting local fighters, battling with rivals and taking over the town of Sirte, now its main base.
That has worried Tunisian authorities, who have built a border trench and tightened controls along nearly 200-km (125 miles) of the frontier with Libya.
Western military experts are training Tunisians to protect a porous border where smuggling has been a long tradition. Ben Guerdan is well-known as a smuggling town.
"There are still some blind spots in intelligence, but they are advancing with the cooperation of neighbouring countries and with the West," said Ali Zarmdini, a Tunisian military analyst.
But Tunisia's North African neigbours worry about the spill over impact of any further Western air strikes and military action against IS in Libya.
After a US air strike killed 40 mostly Tunisian militants in the Libyan town of Sabratha last month, Tunisian forces went on alert for any cross-border incursions.
Just days before the Ben Guerdan attack, Tunisian troops killed five militants who tried to cross from Libya.
But the fact that even after that setback, militants mustered a force of 50 fighters to strike the town shows the group's ability to keep testing the Tunisian military.