PARIS—A machete-wielding man believed to be visiting from Dubai attacked soldiers near Paris's Louvre museum while shouting “Allahu akbar” before being shot and detained, French officials said, unnerving a country that has been repeatedly targeted by terrorist attacks.

The attack took place at 9:50 a.m. Friday as the soldiers were patrolling a shopping mall that leads to an entrance to the world-famous museum, according to French officials. Brandishing two 16-inch machetes, the attacker ran at the soldiers and managed to cut the scalp of one of them, the officials said.

After being knocked to the ground, one of the soldiers opened fire, shooting the assailant several times—including once in the stomach, according to France’s top antiterror prosecutor, François Molins. The man was taken into custody and was receiving medical care for life-threatening injuries, Mr. Molins added.

The alleged assailant, whom authorities believe to be a 29-year-old Egyptian man who lives in Dubai, entered France on a tourist visa on Jan. 26, Mr. Molins said, adding that his passport had visas indicating he had traveled to Saudi Arabia and twice to Turkey in recent years.

The prosecutor didn’t say whether the suspect has links to any known terrorist network, but said his office had opened a terrorist investigation, “in view of the modus operandi and the statements of the attacker.”