WASHINGTON — Rarely has the Trump administration spoken of Iran other than to condemn it as the world’s chief sponsor of terrorism and an aspiring nuclear weapons state. So when the White House woke on Wednesday to images of a possible Islamic State attack on Tehran, it prompted a sharp quandary: How does President Trump condemn the violence without seeming to embrace the victims?

Several administration officials said it took most of the day for the White House to work out the terse, curt wording of a statement that sought to show sympathy for the Iranian public even as it pointedly suggested that the behavior of Tehran’s clerical leaders made its people a target.

“We grieve and pray for the innocent victims of the terrorist attacks in Iran, and for the Iranian people, who are going through such challenging times,” Mr. Trump wrote. “We underscore that states that sponsor terrorism risk falling victim to the evil they promote.”

The statement capped a day during which Mr. Trump thrust himself into the messy politics of Persian Gulf states, trying to also play peacemaker in a bitter dispute between Qatar and other Sunni Muslim neighbors that threatens to splinter a Middle Eastern alliance fighting the Islamic State.