Casually dressed in dark shorts, a necklace and T-shirt, he would have looked like any other young Tunisian among the German, British and Irish sunbathers soaking up the Mediterranean heat on one of Tunisia's long, yellow beaches.

In just five minutes, armed with the black Kalashnikov he had hidden in his beach umbrella, Saif Rezgui unleashed horror across the Imperial Marhaba resort, leaving 39 victims dead among the deck chairs and pool loungers.

It was the worst attack of its kind in Tunisia's modern history. Islamic State claimed responsibility though authorities say Rezgui, a 24-year-old student, was not on any terrorism watch list or a known militant.

Witnesses say the gunman, dressed like a tourist, drew little attention. He opened fire suddenly, making his way from the beach to the pool and hotel, selecting foreigners, pursuing his victims even as they fled indoors.

Rezgui was apparently well aware of the hotel's layout, a security source said. He had time to reload his rifle at least twice before he was finally confronted and shot dead by police outside the hotel.

Panicked tourists fled from the beach, running among the umbrellas, some falling among the white plastic sun loungers, their bodies later to be covered with towels and sheets. Blood was smeared over the steps leading into the hotel.

"It was horror what we saw, he was killing in an incredible way. It was clear he knew the hotel, he was everywhere," said Neil, an English tourist leaving Sousse with his wife. "He took seven minutes killing. They was no sign he was an extremist, he just looked like a normal young man."

A popular tourism destination, Tunisia has emerged from political upheaval after its 2011 uprising against autocrat Zine el-Abidine Ben Ali. Praised for its transition to democracy, the country is also struggling with rising Islamist militancy.

Tunisian authorities were already on the alert, months after two gunmen killed 21 foreign tourists at the Bardo Museum in Tunis, gunning down Japanese, French and Spanish visitors as they arrived by bus.

Like the Bardo attackers, the young Tunisian gunman appears to have fallen prey to extremist recruiters, radicalized and drawn away from his life as a student in a very short time, security sources said.

Since the 2011 uprising, Tunisia has seen radical imams and ultraconservative groups extend their influence, taking over mosques and setting up religious schools in the early turmoil of the country's transition.

More than 3,000 Tunisians are fighting for Islamic State and other groups in Iraq, Syria and neighboring Libya. Some have warned they will return to carry out attacks in their homeland, one of the most secular nations in the Arab world.

By official accounts, Rezgui was a dedicated student from a stable family who enjoyed partying and practiced break-dancing. In a pattern similar to other Tunisia jihadists, he appeared to have come into contact with extremist preachers about six months ago, a senior security source said.

The two Bardo gunmen were also radicalized in their local mosques by hardliners. They were sent to Libya for training and showed no outward signs of their new extremist beliefs.

"He was a good student and always attending class," Prime Minister Habib Essid said. "Our investigations show he didn't reveal any signs of extremism, or ties to terrorists. He wasn't even on a watch list."

'Get out of the way'

It was not the first time Sousse had been targeted. In October 2013, a young Tunisian militant blew himself up on a Sousse beach, killing only himself, after he was refused entry into a hotel. A fellow attacker was caught before he could detonate his bomb among tourists at a popular monument not far away.

The Bardo killings in March had been the worst massacre since the 2002 attack on a synagogue on the tourist island of Djerba, where an al Qaeda suicide bomber killed 21 people.

The Imperial Marhaba is an all-inclusive resort on the long stretch of beach that makes up the Sousse waterfront. A giant pool sits among palm trees set back from the beach.

Packed with European holiday-makers, the resort would have been a desired target for Islamist groups who have attacked North African tourist sites before, seeing them as legitimate targets because of their open Western lifestyles and tolerance of alcohol.

Islamic State praised the gunman's operation against a "bordello".

"I was on the beach when he started shooting. We got everyone back toward the hotel, but he followed us. He targeted the foreigners but not the Tunisians," said Wadia, a waiter.

"When he saw a Tunisian he shouted out 'Get out of the way' and shot at foreigners."