ISTANBUL -- Turkey’s state-run news agency says the gunman who carried out the attack on the Istanbul nightclub was wanted in his home country of Uzbekistan for membership in a “terror organization.”

Anadolu Agency, reporting from Uzbekistan’s capital of Tashkent on Tuesday, said the alleged assailant left the country for Afghanistan six years ago.

The agency based its report on unnamed security sources and added that Uzbek authorities were sharing information with Turkey on the suspect, identified as Abdulkadir Masharipov.

Get Breaking News Delivered to Your Inbox

Masharipov was detained in a police raid in Istanbul late Monday, more than two weeks after the New Year’s attack on the upscale Reina nightclub that killed 39 people.

Istanbul’s governor has said the attack was executed on behalf of the Islamic State of Iraq and Syria (ISIS), which claimed responsibility just a couple days after the massacre.

The suspect, who switched clothes during the attack, fled the scene by blending into the crowd of survivors. He succeeded in evading police for more than two weeks, reportedly collecting his son in a working class neighborhood of Istanbul before hiding out in a luxury apartment at another low-income district.

Photographs widely published in the Turkish media showed a bruised, black-haired man in a gray, bloodied shirt being held by his neck. NTV television said the gunman had resisted arrest.

Hundreds of people were gathered at the swanky Reina nightclub to celebrate the end of a tumultuous 2016 only to become the first victims of 2017. The gunman shot a police officer and a civilian outside the club, before storming the premises.

Turkish Prime Minister Binali Yildirim announced on Tuesday that “the vile terrorist” who attacked the nightclub had been captured. Speaking to reporters in Ankara, he said the “forces behind (the attack) would be revealed in time.”

Moments later, in separate remarks, Istanbul governor Vasip Sahin, gave a full portrait of the suspected killer and the efforts that went into securing his capture.

Sahin named Masharipov, an Uzbek national who was born in 1983 and also operated under the alias Ebu Muhammed Horasani. Turkish media have reported the suspect’s first name as Abdulgadir.

Turkey’s state-run Anadolu Agency said Masharipov traveled to Afghanistan from his home country six years ago and claimed he was on a wanted list in Uzbekistan for membership in a terror group.

The suspect, according to the governor of Istanbul, had trained in Afghanistan and was believed to have entered Turkey in January 2016. He described him as a highly educated terrorist who speaks four languages and had clearly carried out the attack in the name of ISIS.

Masharipov, who was taken into custody late Monday, confessed to carrying out the massacre and his fingerprints matched those of the attacker, Gov. Sahin said.

He can be held for up to 30 days under Turkey’s state of emergency, which was introduced after a failed coup attempt in July, before he is charged and formally arrested. It could take prosecutors several months to prepare for a trial.

The police operation to apprehend Masharipov drew on a review of 7,200 hours of security camera footage and about 2,200 tipoffs from the public. Police searched 152 addresses and 50 people were taken into custody.

Authorities seized nearly $200,000, two guns and two drones during the suspect’s arrest.

“Together with the terrorist, an Iraqi man was detained as well as three women from various countries -- from Egypt and from Africa,” Sahin said. “There is a high chance that they may be connected (to ISIS) because they were staying in the same house.”

The governor said it was believed that they arrived three days earlier at Esenyurt, a low-income neighborhood of Istanbul that has witnessed a construction boom.

AP reporters visited the suspect’s apartment on Tuesday, finding doors with broken locks, food and garbage on the floor and clothes outside of the closets. They also saw a woman’s purse and money of various currencies including Egyptian and Sudanese pounds.

Neighbors of the alleged attacker were in shock to learn of his identity and find their building at the heart of a large-scale police operation. Ali Haydar Demir said he came out of his apartment when he heard the commotion only to be turned back by police officers who told him to close his door.

Demir, who lived on the same floor of the Istanbul complex as Masharipov, said he felt “very bad living in the same building with a person like that.”

Another resident, Sezer Aras, described the situation as a nightmare. He told the AP “he was very close to us, but we had no idea.”

The state-run Anadolu Agency said that the gunman’s 4-year-old son was taken into protective custody.

Hurriyet newspaper earlier reported that the suspect’s wife and 1-year-old daughter were caught in a police operation in the neighborhood of Zeytinburnu on Jan. 12.

In another report citing police officials, the newspaper said the gunman had picked up his son from Zeytinburnu after attacking the nightclub.

Sahin said the boy wasn’t with Masharipov on the night of the police operation, although he had taken the child with him and left his daughter with his wife.

ISIS has claimed responsibility for the nightclub massacre, saying the attack in the first hours of Jan. 1 was in reprisal for Turkish military operations in northern Syria.

Days after the attack, Deputy Prime Minister Numan Kurtulmus said an intelligence agency may have been involved, an assertion he reiterated Monday. But Sahin, when asked about it, declined to comment, saying: “It is too soon to say anything about such connections.”

Anadolu said police also carried out raids on members of a suspected Uzbek IS cell in five Istanbul neighborhoods, and detained several people.

Turkish media also circulated a photograph of the Iraqi suspect lying on the floor face down, hands bound behind his back, with the boot of an unidentified man pressed to the back of his head.

Speaking in Ankara, President Recep Tayyip Erdogan thanked his country’s security and intelligence agencies for their efforts.

“In this country, no one will slip through the net, everyone will be held to account within the limits of the rule of law,” he said.

Turkey, a member of NATO and a partner in the U.S.-led coalition against IS, has endured multiple attacks attributed to the extremist group. IS said the assault on the nightclub was retaliation for Turkey’s military operations in northern Syria.

The country has also witnessed an uptick in violence linked to the resumption of conflict between the Turkish state and Kurdish militants.