When the week ended without the war many feared, Mr. Trump boasted that he had taken out an American enemy. But the struggle between two nations is not really over. Iran may find other ways to take revenge. Iraqi leaders may expel American forces, accomplishing in death what General Suleimani tried and failed to do in life. And in the confusion, a Ukrainian civilian passenger jet was destroyed by an Iranian missile, killing 176 people.

The episode briefly gave Mr. Trump’s allies something to cheer, distracting from the coming Senate impeachment trial, but now he faces questions even among Republicans about the shifting justifications for the strike that he and his national security team have offered. Secretary of State Mike Pompeo initially cited the need to forestall an “imminent” attack and the president has amplified that to say four American embassies were targeted.

But administration officials said they did not actually know when or where such an attack might occur and one State Department official said it was “a mistake” to use the word “imminent.” And some senior military commanders were stunned that Mr. Trump picked what they considered a radical option with unforeseen consequences.

This account, based on interviews with dozens of Trump administration officials, military officers, diplomats, intelligence analysts and others in the United States, Europe and the Middle East, offers new details about what may be the most consequential seven days of the Trump presidency.

If They Enter the Compound, Kill Them

The confrontation may have actually begun by accident. For years, Iran has sponsored proxy forces in Iraq, competing for influence with American troops who first arrived in the invasion of 2003. Starting last fall, Iranian-backed militias launched rockets at Iraqi bases that house American troops, shattering nerves more than doing much damage.

So when rockets smashed into the K1 military base near Kirkuk on Dec. 27, killing an American civilian contractor, Nawres Waleed Hamid, and injuring several others, the only surprise was the casualties. Kataib Hezbollah, the Iranian-backed militia group held responsible, had fired at least five other rocket attacks on bases with Americans in the previous month without deadly results.

American intelligence officials monitoring communications between Kataib Hezbollah and General Suleimani's Islamic Revolutionary Guards Corps learned that the Iranians wanted to keep the pressure on the Americans but had not intended to escalate the low-level conflict. The rockets landed in a place and at a time when American and Iraqi personnel normally were not there and it was only by unlucky chance that Mr. Hamid was killed, American officials said.