Two militants were also killed in the incident.
Visitors from Italy, Germany, Poland, and Spain were among the dead in the midday assault on the Bardo museum near parliament in central Tunis, said prime minister Habib Essid.
Security forces stormed the former palace around two hours later, freeing other tourists held inside, a government spokesman said. One policeman was killed in the police operation.
The second Tunisian killed was a cleaning woman.
EU foreign policy chief Federica Mogherini said “terrorist organisations” were behind the attack.
“The EU is determined to mobilise all the tools it has to fully support Tunisia in the fight against terrorism,” added Mogherini.
Essid said 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 tourists 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 which 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 neighbouring 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 the latter 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,” said Essid. “Nineteen people were killed including 17 foreign tourists. Twenty-two tourists are wounded.”
“Two militants opened fire on the tourists as they were getting off the buses before fleeing into the museum,” one Bardo employee told Reuters at the scene.
An official at the Italian foreign ministry in Rome said two Italians had been wounded in the attack. About another 100 Italians were in the area and were taken to safety by Tunisian police, authorities added.
The museum is known for its collection of ancient Tunisian artefacts and mosaics and other treasures from classical Rome and Greece.
There were no immediate reports that the attackers had copied Islamic State militants in Iraq by targetting exhibits seen by hardliners as idolatrous.