After the latest in a string of deadly attacks to hit the country, President Recep Tayyip Erdogan said the carnage sought to sow chaos, but vowed Turkey would never bow to the threat.
The shooting spree at the waterside Reina nightclub erupted when 2017 in Turkey was just 75 minutes old, as people were seeing out a year of unprecedented bloodshed that saw hundreds die in strikes blamed on militants and Kurdish militants and a failed coup.
The assailant shot dead a policeman and a civilian at the club entrance and then turned his gun on partygoers inside where up to 700 people were ringing in the New Year.
Turkey 'Santa' nightclub attack: What we know
NTV broadcaster said the gunman fired between 120 and 180 rounds in the seven-minute attack, during which many revellers threw themselves into the freezing waters of the Bosphorus to escape death.
One witness spoke of the panic and carnage at the venue, considered one of Istanbul's swankiest. "Just as we were settling down, by the door there was a lot of dust and smoke. Gunshots rang out," professional footballer Sefa Boydas told AFP.
"People were walking on top of people." Italian tourist Maximilien said: "We came here to have a good time today but everything was suddenly transformed into chaos and a night of horror."
Television pictures showed party-goers dressed to the nines - men in suits and women in cocktail dresses - emerging from the exclusive club in a state of shock.
Albert Farhat recounted to Lebanese TV station LBCI the moment the club came under attack.
"Around 1.15, we heard Kalashnikov fire. We thought it might be people who had drunk too much and were fighting but then people started throwing themselves to the ground," said Farhat.
Candles and flowers piled up outside the club in tribute to the victims and its Facebook page has been changed to a black square of mourning.
At least 39 killed in New Year gun attack at Istanbul nightclub
"Our heart is bleeding," wrote the club's owner Mehmet Kocarslan.
The attack evoked memories of the November 2015 carnage in Paris when Islamic State militants unleashed a gun and bombing rampage on nightspots in the French capital, killing 130 people including 90 at the Bataclan concert hall.
Prime Minister Binali Yildirim said the gunman was still at large after slipping away unnoticed after the attack, denying earlier reports a Santa Claus costume had been used as a disguise.
The assailant "left the gun and went away from the scene of the incident," he told reporters in Istanbul. "It was an armed terrorist."
Interior Minister Suleyman Soylu said the gunman had arrived with a gun concealed under an overcoat but subsequently exited the venue wearing a different garment.
Police and intelligence services were beginning to piece together clues about the attack and the public would be informed soon, Yildirim said.
However, he did not pin the blame on any particular group and no one has yet claimed responsibility.
The Firat news agency, seen as close to the outlawed Kurdistan Workers' Party (PKK), quoted PKK leader Murat Karayilan as saying no Kurdish force was involved in the attack.
The state-run Anadolu news agency, citing justice ministry sources, said 38 of the victims had been identified - 27 foreigners and 11 Turks with one body still unidentified. Another 65 people were being treated in hospital.
One survivor, Francois al-Asmar, from Lebanon, told of his lucky escape."I was saved by my passport which I was carrying right near my heart," he told Lebanese television.
The Reina club is a magnet for wealthy foreigners and there were a number of Arabs among the dead and wounded, including Saudis, Jordanians, Iraqis and Tunisians.
France said a dual-national Tunisian-French woman had died along with her Tunisian husband, while India said it had lost two nationals.
A young Arab Israeli woman, 18-year-old Lian Nasser, was killed, according to Israel's foreign ministry.
Canada's Prime Minister Justin Trudeau confirmed that a Canadian woman was killed, while Tass news agency reported that a Russian woman was also among the dead.
Istanbul governor Vasip Sahin said the attacker "targeted innocent people who had only come here to celebrate the New Year and have fun".
The attack crystallised fears New Year celebrations could present a tempting target for extremists.
World prepares to celebrate New Years eve amid terrorist threats
From Sydney to Paris, Rio to London, security had been boosted and at least 17,000 police officers were deployed in Istanbul, some dressed themselves as Santa Claus as cover, according to television reports.
World leaders rushed to condemn the attacks, with Russian President Vladimir Putin saying it was "hard to imagine a crime more cynical than the killing of civilians during a New Year's celebration".
German Chancellor Angela Merkel denounced "an inhumane, sneaky attack on people who wanted to celebrate" while Pope Francis condemned the shooting in his New Year message.
COMMENTS
Comments are moderated and generally will be posted if they are on-topic and not abusive.
For more information, please see our Comments FAQ