The terror suspect accused in the shooting rampage at a nightclub in Istanbul early on New Year's Day trained in Afghanistan before traveling to Turkey and launching his attack, a Turkish official announced Tuesday, hours after police finally pulled the bloodied man from the home where he'd been hiding.

FBI ARRESTS WIFE OF ORLANDO NIGHTCLUB MASSACRE GUNMAN OMAR MATEEN

Abdulkadir Masharipov, the Uzbek national wanted in the murders of 39 people at the Reina nightclub, was arrested after a police raid on his friend's house in Istanbul's Esenyurt district. Turkish media previously had given his first name as Abdulgadir.

Masharipov confessed to the massacre and his fingerprints matched those of the attacker, Istanbul governor Vasip Sahin told reporters, saying the suspect trained in Afghanistan before entering Turkey in January 2016. Sahin said Masharipov was born in 1983, but did not give an exact date.

SIX REPORTED DEAD IN ATTACK ON SECURITY CHECKPOINT IN EGYPT

The Islamic State terror group claimed responsibility for the attack.

A photo released by Turkish media showed Masharipov with cuts on his face, blood on his grey shirt and a dazed look in his eyes. Police had released an image of him -- apparently from a selfie video -- days after the manhunt began.

The suspect had fought back against police trying to arrest him, the private NTV television station reported. Investigators seized nearly $200,000, two guns and two drones during the suspect's arrest.

A Kyrgyz man and three women were detained with Masharipov and were taken to Istanbul's main police headquarters for questioning, the state-run Anadolu Agency reported.

Investigators zeroed in on the gunman's whereabouts four or five days ago but delayed the raid so they could monitor his movements and contacts, NTV also reported.

The station broadcast footage showing plain-clothed police taking away a man in a white top and sweatpants, forcing his head down. The station said the images showed the gunman's Kyrgyz friend being taken to a police vehicle.

The Hurriyet newspaper reported that Masharipov's young son was also in the house when his father was arrested. The boy was taken into protective custody. The paper said the alleged gunman's wife and 1-year old daughter were caught in a police operation on Jan. 12.

The suspect, according to Sahin, speaks four languages and was operating under the alias "Ebu Muhammed Horasani."

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

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

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

Speaking in Ankara, President Recep Tayyip Erdogan said the operation to apprehend the suspect was successful and 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 ISIS, has endured multiple attacks attributed to the extremist group. ISIS claimed the assault on the nightclub was retaliation for Turkey's military operations in northern Syria.

The Associated Press contributed to this report.