The North African nation's Prime Minister called it a cowardly terrorist attack and warned that three suspects were still on the loose.
Tunisian security forces killed two attackers as they ended the hostage siege at the Bardo Museum in Tunis, Prime Minister Habib Essid said. But the death toll, which included 17 tourists and at least one Tunisian security officer, could climb.
Polish, Italian, German and Spanish tourists were among those killed, Essid said, with another 20 foreign tourists and two Tunisians wounded in the attack.
"It's a cowardly attack mainly targeting the economy of Tunisia," the Prime Minister said. "We should unite to defend our country."
The scene played out at a popular tourist destination in the heart of Tunisia's capital in a building linked to where the nation's parliament meets.
Lawmakers there were in the middle of a committee meeting when they heard gunfire erupt.
"The tourists were frightened and they were running in different directions. We opened the doors and we got them to enter to the Parliament," lawmaker Mehrezia Labidi told CNN's Christiane Amanpour.
An administrator told lawmakers to lay down on the ground as a gunbattle broke out between terrorists and police, said Sabrine Ghoubatini, a member of parliament.
It wasn't long before the building was evacuated.
Photos on Twitter showed security forces in bulletproof vests and black helmets and masks, guns drawn, in the area. Authorities set up a large security cordon around the targeted museum.
Rescuers carried wounded tourists away on stretchers.
Essid didn't specify where the attackers came from. Interior Ministry spokesman Mohamed Ali Aroui called them Islamists in remarks on national radio.
Who's behind the attack?
The museum, housed in a 19th century palace, bills itself
as "a jewel of Tunisian heritage." Its exhibits showcase Tunisian art, culture and history, and it boasts a collection of mosaics, including one of the poet Virgil, as well as marble sculptures, furniture, jewels and other items.
No one immediately claimed responsibility for the attack. But it happened just days after a Tunisian jihadist tweeted that a pledge of allegiance to ISIS leader Abu Bakr al-Baghdadi was coming soon, according to the SITE Intelligence Group.
In his message, the jihadi claimed to belong to Jund al-Khilafah in Tunisia, a group that in December pledged allegiance to ISIS, even though that vow didn't seem to have fully registered with the Islamist extremist group. His post comes after an ISIS fighter in Raqqa, Syria
, recently appeared in a video questioning why jihadis in Tunisia had not pledged fealty.
"This raises the possibility that the museum attack could be ISIS' debut on the Tunisian stage, timed to precede a pledge of allegiance from Tunisian jihadis for maximum impact," CNN terrorism analyst Paul Cruickshank said.
Tunisia has seen far less militant violence than other nations in the region that were part of the Arab Spring
uprisings, like neighboring Libya. But it hasn't been immune to it.
The government has been battling a jihadist presence in the Chaambi Mountains
. There have been several apparent political assassinations.
And in February, the country's Interior Ministry announced the arrests of about 100 alleged extremists
and published a video allegedly showing that the group possessed a formula for making explosives and a photograph of ISIS leader Abu Bakr al-Baghdadi.
Up to 3,000 Tunisians are believed to have traveled to Iraq and Syria to fight as jihadists
, more than any other country, according to the International Centre for the Study of Radicalization in London.
"There are hundreds that have returned from the battlefield, yet we haven't seen this kind of activity in Tunisia yet," said Rick Francona, a former U.S. Air Force intelligence officer and CNN military analyst.
"I think it was only a matter of time. And today was the day."
Where Arab Spring took root
Tunisia is where the Arab Spring took root in December 2010, when a poor, 26-year-old man set himself on fire in front of a Tunisian government building after police confiscated his vegetable cart, sparking protests there. Protests throughout the Middle East and North Africa soon followed, with revolutions toppling governments in some countries.
While violence and instability have continued to shake Egypt, Libya and Syria, in many ways, Tunisia has been the exception, with The Economist going so far as to name it "Country of the Year"
"The idealism engendered by the Arab spring has mostly sunk in bloodshed and extremism, with a shining exception: Tunisia," the magazine wrote. "... Its economy is struggling and its polity is fragile; but Tunisia's pragmatism and moderation have nurtured hope in a wretched region and a troubled world."
While it has been more peaceful than other countries, Tunisia has seen its share of violence and political turmoil.
There was cautious optimism after the October 2011 elections -- the country's first since its independence in 1956 -- that involved 60 political parties and thousands of independent candidates vying for seats in the country's new Constitutional Assembly.
In 2013, two opposition leaders were assassinated.
But analysts say Wednesday's attack could have been carried out by people who care more about fomenting terror or establishing a strict Islamic caliphate -- ISIS' aim -- than Tunisian politics.
The country has also been batting socioeconomic problems such as youth unemployment. Young people who can't get jobs are finding that joining extremist groups like ISIS and al Qaeda is a way out.
Up until now, these recruits have largely done their fighting away from home. Experts think that by taking the fight to Tunisia, they'll hurt their cause not just by hurting the tourism-reliant economy, but also by alienating most of their countrymen.
"They're already isolated and marginalized, and they further isolate and marginalize themselves by these actions," said Mubin Shaikh, a former undercover counterterrorism operative. "... This will just further isolate and alienate these groups from the rest of the public."