BAGHDAD — A U.S. airstrike killed Iranian Quds Force commander Qasem Soleimani in Baghdad late Thursday, the Pentagon said, a dramatic escalation of tensions between the two countries that could lead to widespread violence in the region and beyond.

Defense Secretary Mark T. Esper said the Pentagon had taken “decisive defensive action” against Soleimani, the revered military figure who had close links to a network of armed groups backed by Iran across the Middle East and, according to the United States, bore responsibility for hundreds of American deaths.

“Gen. Soleimani was actively developing plans to attack American diplomats and service members in Iraq and throughout the region,” Esper said in a statement. “This strike was aimed at deterring future Iranian attack plans.”

Iran confirmed the death of one of its most active military figures and vowed revenge against the United States. Iran’s supreme leader, Ayatollah Ali Khamenei, said in a statement Friday that Soleimani’s death was “bitter” but that “the final victory will make life more bitter for the murderers and criminals.”

Iran’s defense minister, Brig. Gen. Amir Hatami, added that the attack would be met with a “crushing” response. Foreign Minister Mohammad Javad Zarif called the strike an “act of international terrorism” and, in a message on Twitter, said the United States “bears responsibility for all consequences of its rogue adventurism.”

Earlier, Iraqi militia officials and the country’s state TV channel announced that Soleimani had been killed in an airstrike alongside a top Iraqi militia leader just outside the country’s main airport. The Iraqi, Jamal Jaafar Ibrahimi, who is better known by his nom de guerre, Abu Mahdi al-Muhandis, is closely associated with attacks against the United States dating to 1982.

In a statement, Iraqi Prime Minister Adel Abdul Mahdi condemned the U.S. “assassination,” adding that the killing of the Iraqi militia leader was an act of aggression against Iraq and a breach of the conditions under which American forces operate in the country.

A video circulated by Shiite militia groups showed, accompanied by the sound of wailing, the crumpled wreckage of the vehicle in which Soleimani purportedly was traveling. A photograph claimed to show his bloodied, ash-covered hand wearing the same blood-red ring seen in earlier photos of him alive.

A U.S. official, who like others spoke on the condition of anonymity because he was not authorized to comment on the record, said the attack was conducted by a U.S. drone and struck a two-car convoy carrying Soleimani and others on an access road near Baghdad International Airport. At least half a dozen people were believed to have been killed.

Senior officials with the Popular Mobilization Forces, as the Iraqi militia groups are known, lamented the deaths in messages circulating on WhatsApp. “May God reward you for the loss of the brave leaders, Hajj Soleimani and Hajj Muhandis. May God accept them as martyrs in his vast mercy,” wrote Ahmed al-Assadi, the chief spokesman of the Popular Mobilization Forces, many of which are seen as being funded and directed by Iran.

Despite a long period of increasing tension between Iran and the Trump administration, which has vowed a tougher stance on Tehran’s support for proxy groups, the attack against an incomparable figure in Iran’s security establishment came as a surprise to many analysts, in part because it was seen as likely to ignite a significant Iranian response.

Ilan Goldenberg, who worked on Middle East issues during the Obama administration, characterized the move as a “massive game changer” in the region.

“Iran will seek revenge. It may escalate in Iraq, Lebanon, the gulf or elsewhere. It may attempt to target senior U.S. officials,” said Goldenberg, now a scholar with the Center for a New American Security. “Unfortunately, I highly doubt the Trump administration has thought out the next step or knows what to do now to avoid a regional war.”

The attack, which Esper said was authorized by President Trump, raises fresh questions about the president’s approach to the Middle East. While Trump has employed bellicose rhetoric and authorized several strikes against the Syrian government, an ally of Tehran, he has repeatedly voiced his desire to get the United States out of costly wars in the region.

The attack appeared intended to cripple a force that has been the vanguard of Iran’s decades-long effort to shape events in the Middle East in its favor. Soleimani, who rose from poverty in southeast Iran, joined the Revolutionary Guard Corps as a young man, and later took control of the Quds Force, its special operations wing, in the late 1990s.

Under his command, the force expanded its support for armed groups across the region, including in Iraq, where U.S. officials blamed Iranian-backed militias for killing at least 600 American troops following the 2003 U.S. invasion. The force also was named in a 2011 plot to assassinate a Saudi diplomat at a Washington restaurant. In recent years Soleimani was regularly seen making visits to affiliated militias in Iraq, Syria and elsewhere, demonstrating not just his military influence but significant diplomatic clout.

But after the Islamic State took over vast swaths of Iraq in 2014, Iraqi militias linked to Soleimani were temporarily aligned with U.S. counterterrorism objectives in Iraq as they played an important role in defending Baghdad and eventually crushing the militant group’s grip on Iraqi cities and towns.

“Soleimani was a popular figure in Iran, and more importantly, was their key instrument used to project power in the region,” said Marc Polymeropoulos, a former CIA official with extensive counterterrorism experience overseas who retired this year. “For the legitimacy of the Iranian regime, a forceful response against the U.S. should be expected. The American public needs to understand that we may lose American lives after this act.”

The congressional response to the strike, with few exceptions, was split along party lines, with leading Republicans cheering Trump for decisively meting out “justice” against Iran while leading Democrats warned that the “disproportionate” strike would lead to an “almost inevitable escalation.”

Thursday’s airstrike appears to have been set in motion by a Dec. 27 rocket attack that killed a U.S. contractor on a base in the Iraqi city of Kirkuk, an assault the Pentagon blamed on Kataib Hezbollah, an Iraqi militia closely associated with Iran. After the attack, Trump authorized airstrikes on militia targets in Iraq and Syria, which in turn prompted militiamen to attempt to breach the U.S. Embassy in Baghdad.

While the episode was defused by Thursday, it laid bare the combustible state of U.S.-Iran ties and the potential vulnerability of American personnel in the region.

Speaking to reporters earlier in the day, Esper said the Pentagon was ready to take military action to preempt militia attacks. “The game has changed,” he said. “And we’re prepared to do what is necessary to defend our personnel and our interests and our partners in the region.”

A U.S. official said discussion of the strike began after the contractor’s death. On Wednesday, Secretary of State Mike Pompeo abruptly canceled a planned trip to Eastern Europe, citing the need to stay in Washington “to continue monitoring the ongoing situation in Iraq and ensure the safety and security of Americans in the Middle East.”

Pompeo wanted to be near Trump to advise him on the ongoing situation, said a senior U.S. official familiar with the matter, speaking on the condition of anonymity to discuss internal matters.

It’s not clear what action the Trump administration will take to protect U.S. diplomats and military personnel from Iranian retaliation.

Officials said they were taking steps to defend Americans. “We’re well aware of the possibility of an Iranian response,” one official said.

Already in the past week, the administration has deployed 750 troops from a special quick-action battalion from the 82nd Airborne Division to Kuwait, a staging ground for forces going into Iraq. About 100 Marines were sent into Baghdad to protect the embassy after the militia siege.

About 5,000 U.S. troops are stationed in Iraq as part of efforts to combat the remnants of the Islamic State and support Iraqi security forces. While the number of diplomats there is far fewer than it has been in past years, hundreds of embassy personnel were forced to shelter in safe rooms during this week’s siege.

In Baghdad, according to officials familiar with the situation, diplomats at the sprawling U.S. Embassy began packing bags in the event of a potential evacuation order.

An official at Iraq’s elite counterterrorism force said it had been ordered to close all entrances to the Green Zone in Baghdad that houses major government ministries and foreign embassies and to deploy inside it.

Earlier on Thursday, Gen. Mark A. Milley, chairman of the Joint Chiefs of Staff, said the embassy compound, which contains hardened offices and residences and occupies more than 100 acres in Baghdad’s international zone, remained secure.

“There is sufficient combat power there, air and ground, that anyone who attempts to overrun that will run into a buzz saw,” Milley said.

The airstrike caps more than a year of building tension between the United States and Iran. In 2018 the Trump administration withdrew from the Iran nuclear deal, and it has since imposed new sanctions that have devastated the Iranian economy. In June, Trump authorized and then called off airstrikes in Iran after Tehran’s downing of an American surveillance drone.

Thursday’s airstrike is likely to further strain U.S. relations with the Iraqi government, which includes senior officials seen as having strong allegiances to Tehran and which has been pulled between its chief Western ally and its powerful neighbor to the east.

The Iraqi government has been in crisis for months amid massive popular protests focused on widespread corruption and, to a lesser extent, Iranian influence in Iraq. The mass mobilizations prompted Prime Minister Mahdi to resign late last year, though he remains in office in a caretaker capacity.

Oil prices jumped on Asian markets. U.S. crude oil rose about 1.2 percent initially, while Brent crude, more commonly used around the world, rose 1.3 percent. Oil futures on the New York Mercantile Exchange spiked even higher, with U.S. oil prices jumping as much as 4 percent for a February delivery.

Sly reported from Beirut. Ryan and Hudson reported from Washington. Dan Lamothe, Shane Harris, Carol Morello and Karoun Demirjian in Washington contributed to this report.