The suspected gunman who attacked an Istanbul nightclub on New Year's Eve, killing 39 people, has confessed and his fingerprints are a match, Turkish authorities said.
They identified him as an Uzbek national who trained in Afghanistan and staged the attack for the Islamic State group.
The gunman shot a policeman and a civilian outside the Reina nightclub before entering the building on the banks of the Bosporus and unleashing a hail of bullets on hundreds of people gathered to celebrate the end of 2016.
A total of 39 people lost their lives and dozens of others were wounded.
Most of the victims came from the Middle East.
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 took weeks, reportedly collecting his son in a working-class neighbourhood 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 bloodied shirt being held by his neck.
NTV television said the gunman had resisted arrest.
Prime Minister Binali Yildirim announced 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.
He named the alleged killer as Abdulkadir 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 travelled 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 IS.
Masharipov, who was taken into custody late on Monday, confessed to carrying out the massacre and his fingerprints matched those of the attacker, Mr 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 him drew on the review of 7,200 hours of security camera footage and about 2,200 tip-offs from the public.
Police searched 152 addresses and 50 people were taken into custody.
Authorities seized nearly 200,000 US dollars (£165,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," Mr Sahin said.
"There is a high chance that they may be connected (to IS) 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 neighbourhood 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.
Neighbours 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.
Mr 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 four-year-old son was taken into protective custody.
The Hurriyet newspaper earlier reported that the suspect's wife and one-year-old daughter were caught in a police operation in the neighbourhood of Zeytinburnu, a working class district of Istanbul, on January 12.
In another report citing police officials, the newspaper said the gunman had picked up his son from Zeytinburnu after attacking the nightclub.
Mr Sahin said the boy was not with the gunman on the night of the police operation, although he had taken the child with him and left his daughter with his wife.
IS has claimed responsibility for the nightclub massacre, saying the attack in the early hours of January 1 was in reprisal for Turkish military operations in northern Syria.
The man identified as the suspect had been on the run since the attack.
Days after the attack, Deputy Prime Minister Numan Kurtulmus said an intelligence agency may have been involved, an assertion he reiterated on Monday.
However, Mr Sahin declined to comment, saying: "It is too soon to say anything about such connections."
Anadolu said police have also carried out raids on members of a suspected Uzbek IS cell in five Istanbul neighbourhoods, and detained several people.
Photographs from raids, widely published in the Turkish media, showed a bruised, black-haired man in a bloodied shirt being held by his neck.
NTV television said the gunman had resisted arrest.
Turkish media also circulated a photograph of the suspect lying on the floor, hands bound behind his back with the boot of an unidentified man pressed to the back of his head.
Turkey, a member of Nato and a partner in the US-led coalition against IS, has endured multiple attacks attributed to the extremist group.
The country has also witnessed an increase in violence linked to the resumption of conflict between the Turkish state and Kurdish militants.