By Daren Butler

ISTANBUL (Reuters) - Turkish authorities have captured the gunman who killed 39 people in an Istanbul nightclub on New Year's Day, an Uzbek national they said was trained in Afghanistan and had clearly acted on behalf of Islamic State.

The suspect, named by Istanbul Governor Vasip Sahin as Abdulgadir Masharipov, was caught in a police raid late on Monday in a hideout in an outlying Istanbul suburb after a two-week manhunt.

Masharipov, who was captured with four others, had admitted his guilt and his fingerprints matched those at the scene, Sahin told a news conference.

"He knew four languages and was well-educated," Sahin said, adding he was born in 1983 in Uzbekistan and received training in Afghanistan.

There were strong indications he entered Turkey illegally through its eastern borders in January 2016 and it was clear the attack was carried out on behalf of Islamic State, Sahin said.

The jihadist group claimed responsibility a day after the mass shooting, saying it was revenge for Turkish military involvement in Syria.

Masharipov was captured with an Iraqi man and three women from Africa, one of them from Egypt, in the Esenyurt district on Istanbul's western outskirts, about 30 km (19 miles) from the Reina nightclub.

Two pistols, mobile phone SIM cards, two drones and $197,000 in cash were also seized, Sahin said.

Dogan news agency published a photo of the alleged attacker with a black eye, a cut above his eyebrow and bloodstains on his face and t-shirt. It broadcast footage showing plain-clothes police leading a man in a white sweater to a waiting car.

He was being questioned at Istanbul police headquarters, while other people were detained in raids across the city targeting Uzbek Islamic State cells, the state-run Anadolu news agency said.

"WE SAID OUR GOODBYES"

The gunman appeared to have repeatedly changed addresses before and after the attack. Remaining in Istanbul, he evaded a 16-day nationwide manhunt that included operations in cities from Izmir on the Aegean coast, to Konya in central Anatolia, and Hatay near the southern border with Syria.

"Five addresses were tracked and operations were carried out against them. He was found at one of the five," Sahin said.

Masharipov and those seized with him late on Monday had moved to the Esenyurt address around three days ago, he said.

Neighbours in the modern, five-storey apartment building where he was found said they had never met him, although they had seen the African women who lived with him. A woman living directly below told Reuters the apartment had previously been lived in by seven or eight Syrians, who left late last summer.

Masharipov first rented an apartment in Basaksehir, another outlying Istanbul district, before switching addresses a day or two before the attack, the Istanbul governor said.

The Hurriyet newspaper said he was married with two children, was a dual Uzbek-Tajik national and spoke Russian, Arabic, Chinese and Turkish, as well as Uzbek. He received two years training in Afghanistan and Pakistan and was believed to have entered Turkey via Iran, it said.

His wife was detained last week in a raid in Maltepe, a coastal district on the Asian side of Istanbul, and their one-and-a-half year-old daughter was taken into care, Hurriyet said.

"We said our goodbyes and he left the house (on the night of the attack)," it quoted her as saying in a statement to police.

About 50 people have been detained in raids on 152 addresses since the shooting. Investigators analysed 7,200 hours of camera footage in the search and police received more than 2,000 tip-offs, Sahin said.

"WAR WITH TERROR" WILL CONTINUE

"I congratulate our police who caught the perpetrator of the Ortakoy massacre," Deputy Prime Minister Numan Kurtulmus, who is also the government spokesman, said on social network Twitter.

"Our war with terror and the powers behind it will continue to the end," he said.

On Jan. 1, the attacker shot his way into the nightclub and opened fire with an automatic rifle. He reloaded his weapon several times and shot the wounded as they lay on the ground.

Turks as well as visitors from several Arab nations, India and Canada were among those killed in the attack.

NATO member Turkey is part of the U.S.-led coalition against Islamic State and launched an incursion into neighbouring Syria in August to drive the radical Sunni militants, and Kurdish militia fighters, away from its borders.

The jihadist group has been blamed for at least half a dozen attacks on civilian targets in Turkey over the past 18 months. But, other than assassinations, the new year attack was the first it has directly claimed.

The shooting in Istanbul's Ortakoy neighbourhood, an upscale district on the Bosphorus shore, followed a year in which Turkey was shaken by a series of attacks by radical Islamist and Kurdish militants and by a failed coup.

President Tayyip Erdogan has said the attack, which targeted a club popular with local celebrities and moneyed foreigners, was designed to divide the largely Sunni Muslim nation.

(Additional reporting by Maria Tsvetkova in Istanbul; Editing by Nick Tattersall, Timothy Heritage, Anna Willard)