(CNN) A French police officer is fighting for his life after shooting a gunman who went on a deadly rampage in southern France, authorities said Friday.

The gunman killed three people and wounded 16 others when he stole a car, fired at police officers and took hostages in a supermarket in what authorities are treating as a terrorist attack.

Interior Minister Gérard Collomb said the officer, a lieutenant colonel with the National Police, offered to exchange himself for one of the female hostages being held inside the Super U supermarket in Trèbes.

When the officer went inside the supermarket, shots were fired. The lieutenant colonel had his phone on, so police could hear his interactions with the gunman, Collomb said. As soon as they heard gunfire, police went in.

The gunman was killed and the officer, identified by a source close to the investigation as Arnaud Beltrame, was seriously wounded.

The Interior Ministry identified the gunman as 26-year-old Redouane Lakdim, who was known to authorities for minor crimes including drug offenses, Collomb said.

An investigation has been opened into murder and attempted murder "in connection with a terrorist in Trèbes," the French prosecutor's office told CNN.

Police have taken into custody a woman who was "very close" to Lakdim and "shared his life," prosecutor François Molins told reporters at a news conference in Carcassonne.

Police respond to the hostage situation Friday in the southern French town of Trèbes.

"Our country has suffered an Islamist terrorist attack," President Emmanuel Macron told reporters. "We've paid the price of the danger of the terrorist threat in blood for several years.

"I want to state my absolute determination to the nation to lead this fight."

The ISIS-affiliated Amaq News Agency said that the gunman was a "soldier" of the militant group and that he had responded to its call to attack coalition countries, a reference to the U.S.-led coalition fighting ISIS in Syria and Iraq. Amaq offered no evidence ISIS had any contact with the attacker.

"This attack has been claimed by ISIS, and it is currently being analyzed," Macron said.

A local prosecutor said the attack appeared to be "ISIS-inspired," CNN affiliate BFM TV reported. Witnesses said the man shouted, "I am a Daesh soldier," using another name for ISIS.

A BFM TV reporter at the scene said the attacker claimed to be an ISIS soldier and demanded the release of Salah Abdeslam , the suspected mastermind of the November 2015 Paris attacks

CNN could not independently confirm the gunman made the demand. Abdeslam is on trial in Belgium.

France has faced a string of terrorist attacks in recent years, including the Paris attacks and other smaller-scale assaults. The country remained under a state of emergency for about two years after the Paris attacks. It was lifted late last year.

How the rampage began

The violence unfolded when the attacker stole a car, killing one person in the vehicle and gravely wounding another, said Collomb, the interior minister.

The gunman then shot at four National Police officers who were jogging in Carcassonne. The driver tried to run the officers down. One of them was wounded, but he is not in serious condition.

The gunman proceeded to the Super U market. He entered the market, firing his weapon and killing two people. He took several others hostage.

Police found the car, and SWAT teams surrounded the market.

Collomb said a National Police lieutenant colonel shot the gunman after offering himself for at least one of the hostages.

The order of the shots was unclear, Collomb said. Police raided the store after hearing gunshots.

Two others were wounded at the market, an Interior Ministry spokesman said.

"People were absolutely calm before and never thought that there could be an attack in a town like this," Collomb told reporters, adding that the risk of terrorism in France remained "very high."

A Portuguese citizen was among the wounded, a spokesman for Portugal's Foreign Ministry told CNN.

Madeline Fuhrman, a witness, said that Carcassonne had gone quiet after the attack.

"It's totally deserted now. Absolutely every shop is closed. There's not a soul in sight," she said.

A supermarket employee who fled as shots were fired told BFM that he and about 50 others had been on lockdown in a car park.

French police and firefighters secure parts of Trèbes on Friday.

Macron hails heroic actions of officer

Macron, who was in Belgium for a summit of European Union leaders, returned to France and spoke to reporters after a crisis meeting at the Interior Ministry.

French police block access to Trèbes as they respond to the hostage situation Friday.

The French leader hailed law enforcement's response.

"The forces of order intervened with remarkable speed, both to contain the individual who hid away with his hostages, to help one of the victims who was still alive who he had left in the surrounding area," Macron said.

He singled out the officer who volunteered to be traded for a hostage and saluted his bravery.

"He has saved lives, and done his role and his country proud. He is currently fighting for his life, and all our thoughts are with him and his family," Macron said.

Macron said the investigation should answer several questions -- when and how the gunman was radicalized and where he got a weapon.

He said French soldiers overseas risk their lives "to reduce this threat."

CORRECTION: This story has been updated to correct the spelling of the interior minister's name and to correct the location of witness Madeline Fuhrman.