At least 19 tourists were reported dead in a Tunisia museum.
Visitors from Italy, Germany, Poland and Spain were among the dead in the noon assault on the Bardo museum near parliament in central Tunis, Prime Minister Habib Essid said.
Security forces stormed the former palace around two hours later, killed two militants and freed other tourists held hostage inside, a government spokesman said. One policeman was killed in the police operation.
European Union foreign policy chief Federica Mogherini said Islamic State militants, who have become particularly active in neighboring Libya, were behind the attack. "The EU is determined to mobilize all the tools it has to fully support Tunisiain the fight against terrorism," she added.
Prime Minister Essid declared in a national address:
"All Tunisians should be united after this attack which was aimed at destroying the Tunisian economy."
Television footage showed dozens of people, including elderly foreigners and one man carrying a child, running for shelter in the compound, covered by security forces aiming rifles into the air.
The attack on such a high-profile target is a blow for the small North African country that relies heavily on European tourism and has largely avoided major militant violence since its 2011 uprising to oust autocrat Zine El-Abidine Ben Ali.
Tunisia's uprising inspired "Arab Spring" revolts in neighboring Libya and in Egypt, Syria and Yemen. But its adoption of a new constitution and staging of largely peaceful elections had won widespread praise and stood in stark contrast to the chaos that has plagued those countries.
Authorities did not immediately identify the gunmen.
But several Islamist militant groups have emerged in Tunisia since the uprising and authorities estimate about 3,000 Tunisians have also joined fighters in Iraq and Syria -- raising fears they could return and mount attacks at home.
"Two terrorists disguised in military clothes got into the parliament building, then the museum where they attacked tourists. Nineteen people were killed including 17 foreign tourists. Twenty-two tourists are wounded," the prime minister said.