By Daren Butler

ISTANBUL, Jan 17 (Reuters) - Turkish police have captured the gunman who killed 39 people in an Istanbul nightclub on New Year's Day after a two-week manhunt, authorities said on Tuesday, seizing him with four other suspects at a hideout in an outlying suburb of the city.

Istanbul Governor Vasip Sahin named the man as Abdulgadir Masharipov and said he was born in 1983 in Uzbekistan and received training in Afghanistan. He had admitted his guilt and his fingerprints matched those at the scene, Sahin said.

"He knew four languages and was well-educated," Sahin told a news conference after Masharipov's capture overnight.

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.

Masharipov was seized with an Iraqi man and three women from Africa, one of them from Egypt, in the Esenyurt district on Istanbul's western outskirts.

"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 added.

Dozens of people have previously been detained in connection with the attack for which Islamic State has claimed responsibility, saying it was revenge for Turkish military involvement in Syria. urn:newsml:reuters.com:*:nL5N1EU0YT

On Jan. 1, the attacker shot his way into the Reina nightclub then opened fire with an automatic rifle, reloading his weapon half a dozen times and shooting 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 urn:newsml:reuters.com:*:nL5N1ET147 .

Masharipov was caught in an apartment at a housing complex in Esenyurt at around 11 pm (2000 GMT) on Monday.

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 t-shirt to a waiting car.

The suspect was being questioned at Istanbul police headquarters, while other people were detained in raids across the city targeting Uzbek Islamic State cells, Anadolu said.

The shooting in Istanbul's Ortakoy neighbourhood, an upscale district on the Bosphorus shore, came after a year in which NATO member 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, had been being exploited to try to divide the largely Sunni Muslim nation.

(Editing by Nick Tattersall/Jeremy Gaunt) ((daren.butler@thomsonreuters.com; +90-212-350 7122; Reuters Messaging: daren.butler.thomsonreuters.com@reuters.net))