(Jason Aldag/The Washington Post)

The Islamic State claimed responsibility Monday for a deadly rampage at an Istanbul nightclub on New Year’s Eve, an assault by a single gunman that killed dozens of people and served as an ominous reminder of the consequences of Turkey’s expanding war against the Islamic militants in Syria.

A statement posted online said that a “heroic soldier of the caliphate” had attacked the nightclub with grenades and a rifle in ­“revenge for God’s religion” and in response to orders from the Islamic State’s leader, Abu Bakr al-Baghdadi, who recently urged his followers to attack Turkey.

Baghdadi, in an audio message released in November, denounced Turkey for carrying out military operations against the group, which has lost wide swaths of the once-sprawling territory it controlled across Iraq and Syria.

Sunday’s massacre underscored the group’s ability to continue to stage devastating attacks. It was the first major operation against civilians in Turkey that was formally claimed by the Islamic State, confronting the beleaguered government of Turkish President Recep Tayyip Erdogan with a startling new threat.

Turkey, a NATO member and close ally of the United States, has experienced a rash of attacks by various actors in recent months, including one attributed to — but not claimed by — the Islamic State. However, it has mostly escaped the large-scale attacks on civilians the Islamic militants have visited on neighboring countries and Europe in recent years, in large part because Sunni militants viewed Turkey as an important passageway to the battlefields of Syria.

During the early years of Syria’s civil war, militants traveled from all over the world to Turkish airports and then stole across the border, facing little scrutiny from the Erdogan government, which was supporting a rebel insurgency against Syrian President Bashar al-Assad.

Turkish officials have strongly denied that they facilitated militants’ passage.

More recently, though, Turkey has become a more attractive target for the militants as the Turks have tightened control of the border and escalated its pursuit of the Islamic militants, including with military operations inside Syria.

In its brutal efficiency, the nightclub attack highlighted the resilience of the Islamic State, even as the group’s leaders are hunkered down in its remaining strongholds under a withering military attack by a U.S.-led coalition that has rolled back its territorial gains, shut off its sources of revenue and killed tens of thousands of its fighters.

Brian Fishman, a counter­terrorism researcher and the ­author of “The Master Plan: ISIS, al-Qaeda, and the Jihadi Strategy for Final Victory,” said that while military gains against the Islamic State were significant, there has been “excessive optimism in some circles that this is going to turn off the spigot of terrorist attacks elsewhere.”

“The scope of this organization is so big that it can suffer significant territorial losses and still build a bench of supporters that are going to be able to operate as underground cells and smaller networks that are very effective,” he said.

The Istanbul gunman remained at large Monday, but officials said investigators had obtained his fingerprints and a basic description. Police released a blurry photograph of a man they said was the assailant and who Turkish media reported had arrived at the nightclub in a taxi.

He retrieved a Kalashnikov assault rifle from the suitcase he was carrying on the night of the assault, according to reports in two Turkish newspapers, and opened fire outside before throwing hand grenades into the crowd.

The gunman struck “one of the most famous nightclubs in the city,” the Islamic State’s statement said Monday, boasting that it was where “Christians celebrate their pagan holiday.”

The venue, called Reina, is perched on the Bosporus and is popular with Istanbul’s elite, including musicians and soccer players. Hundreds of revelers were at the club when it was attacked at 1 a.m. Sunday, and surviors described scenes of horror as bodies slumped around them and others jumped into freezing water to escape.

Citizens of more than a dozen countries, including Turkey, were killed in the carnage. Many were from the Middle East and North Africa. Russian, Indian, Canadian, German and French nationals also were killed.

One American was wounded in the attack. William Jacob Raak, 35, of Greenville, Del., told NBC News that he survived the attack by playing dead, staying silent and ­motionless after being shot, according to the Associated Press.

On Monday, the state-run Anadolu news agency said that eight people had been detained in connection with the attack, but it provided no details.

It was the latest in a string of major attacks on civilian and military targets across Turkey that have left the country on a war footing and raised questions about the government’s ability to meet the threats.

The government, which for years touted a foreign policy summed up with the slogan “zero problems with neighbors,” now seems consumed by confrontations, including an escalating war with Kurdish militants and a growing crackdown on domestic opponents. Along with the military offensive in Syria, Turkey has sent troops to Iraq, sparking a war of words last year with the Iraqi government.

Violence that racked Turkey in December alone underscored the dangers. In Istanbul, twin bombings killed 44 people outside a soccer stadium, in an attack claimed by Kurdish militants. Less than a week later, another bomb tore through a bus carrying off-duty soldiers, killing 13.

On Dec. 19, a Turkish policeman gunned down Russia’s ambassador to Turkey at an art exhibition, while railing against the bloodshed in Syria.

While the New Year’s attack was the first assault on Turkish civilians formally claimed by the Islamic State, the group is thought to be responsible for other attacks, including a deadly assault on Istanbul’s Ataturk Airport in June.

In its statement, the Islamic State said that the nightclub massacre, which left 39 people dead, was revenge for “Muslim blood spilt” by Turkish “airstrikes and artillery,” an apparent reference to the offensive in Syria.

Nearly 40 Turkish soldiers have been killed there since August, including two who were burned alive in a gruesome video released by the Islamic militants last month.

“Turkey’s security forces, to their credit, have been cracking down” on Islamic militant networks, said Selim Koru, an analyst at the Economic Policy Research Foundation of Turkey, based in Ankara. But “this government wants to keep the fight against [the Islamic State] as quiet as it can.”

Erdogan, he said, has a strong Islamist following and risks alienating that support if he goes too hard on the Islamic militants. But the Islamic State “keeps calling out the government and making a lot of noise,” he said, forcing Erdogan’s hand.

Taken together, the deadly attacks have raised fears that Turkey’s crackdown on the Islamic State may have come too late.

The challenges Turkey faces from the Islamic militants “would be difficult for any country,” Fishman said. The country shares long borders with Syria and Iraq, both destabilized by years of war and hosting thousands of militants.

After years of allowing the militant networks to crisscross Turkey, he said, “it’s very difficult to shut that down.”

Fahim reported from Cairo. Heba Habib in Stockholm contributed to the report.