Japanese, Italian, Spanish and British visitors, as well as three Tunisians, were among the victims of the attack, which took place in the heavily guarded parliament compound of a country largely spared the violent aftermath of the Arab Spring.
Officials did not confirm the militants' claim of responsibility, but said they had identified two gunmen shot dead by security forces after the shootings, which targeted tourist buses visiting the Bardo museum on Wednesday.
The two gunmen were trained at a jihadist camp in Libya, the Tunisian government said. Interior ministry official Rafik Chelli said the two men had been recruited at mosques in Tunisia and travelled to Libya in September.
Cruise liner MSC Cruises said 12 of its passengers, including Colombians, French and a Belgian, were among the dead, while a Spanish couple was found alive on Thursday after hiding all night in the museum.
The assault -- the most deadly attack involving foreigners in Tunisia since a 2002 suicide bombing in Djerba -- came at a fragile moment for a country just emerging to full democracy after its pioneering popular uprising four years ago.
It is heavily reliant on foreign tourists to its beach resorts and desert treks, and the government was about to tackle politically sensitive reforms aimed at boosting economic growth.
Islamic State, which has declared a caliphate in large parts of Iraq and Syria and is active in Tunisia's chaotic neighbour Libya, praised the two attackers in an audio recording as "knights of the Islamic State" armed with machineguns and bombs.
Tunisians make up the one of the largest contingents of foreign fighters in Syria, Iraq and Libya, and their homeland's young democracy, which has cracked down on militancy at home, was a clear potential target.
"We tell the apostates who sit on the chest of Muslim Tunisia: Wait for the glad tidings of what will harm you, o impure ones, for what you have seen today is the first drop of the rain," the Islamic State audio said in Arabic.
The two dead militants were identified as Tunisians, Hatem al-Khashnawi and Yassin al-Abidi. Two local newspapers reported Abidi had spent time in Iraq and Libya, but officials did not confirm that.
Tunisia's Prime Minister Habib Essid said Abidi had been under surveillance but "not for anything very special".
"We have identified them, it is indeed these two terrorists," the premier told French RTL radio earlier on Thursday. "Their affiliation is not clear at the moment."
ARMY TO GUARD LARGE CITIES
Tunisian authorities said they had arrested four people directly linked to the attack and five others with indirect ties. A security source said two family members of one of the gunmen were among those held. "We arrested the father and the sister of the terrorist Hatem Al-Khashnawi in the their home in Sbiba City," the source told Reuters.
The president's office said the army would be deployed to the streets as part of increased security following the attack. "After a meeting with the armed forces, the president has decided large cities will be secured by the army," it said.
The number of foreign tourists killed rose to 20 from 17, the health minister said. London said a British woman was among the dead in shootings it called cowardly and despicable.
Spain said the woman in the couple who emerged from the museum alive on Thursday was pregnant. "They were hidden this whole time, without daring to make a phone call, probably because they were anxious and scared, as you can imagine," Spanish Foreign Minister Jose Manuel Garcia-Margallo said.
Four years after a popular revolt toppled autocrat Zine El-Abidine Ben Ali, Tunisia has completed its transition to democracy with free elections, a new constitution and compromise politics between secular and Islamist parties.
But security forces are battling Islamist militants including Ansar al Sharia, which is listed as a terrorist group by Washington, and Okba Ibn Nafaa, a brigade of al Qaeda-affiliated fighters operating in the Chaambi mountains along the Algerian border.
The fight against these militants may have played a role in prompting the museum attack, said Geoff Porter, security analyst at North Africa Risk Consulting. "Increasing pressure on terrorist activities in the Djebel Chaambi region may have squeezed the balloon, with terrorists seeking softer targets with more symbolic impact," he said.
FOREIGN FIGHTERS
A former commander in Tunisia's Ansar al Sharia was killed last week fighting for Islamic State in Libyan city of Sirte. He had been running training and logistics, security sources said.
The United States said it could not confirm Islamic State was responsible but that the attacks were consistent with the group's actions. It offered Tunisia counterterrorism help.
An earlier Islamic State news broadcast distributed online also mentioned the Tunis attack, describing the perpetrators as Mujahideen and asking God to receive them as martyrs in paradise. But it did not claim direct responsibility.
A social media account linked to Okba Ibn Nafaa brigade also provided details of the attack, without a direct claim of responsibility. But that group has also issued conflicting statements in the past about its position on Islamic State.
The Bardo attack appeared squarely aimed at Tunisia's economy, with tourism accounting for seven percent of gross domestic product. The government estimates that loses this season for the tourism sector would reach $700 million.
Two German tour operators said they were cancelling trips from Tunisia's beach resorts to Tunis for a few days and Accor, Europe's largest hotel group, said it had tightened security at its two hotels in Tunisia. Italy's Costa Cruises, a unit of Carnival Corp CCL.N, cancelled stops in Tunisia.