WASHINGTON — Secretary of State Mike Pompeo on Wednesday canceled a weeklong trip to Ukraine and four other nations to stay in Washington and monitor tensions in Iraq after protesters broke into the United States Embassy compound in Baghdad and wrecked parts of it, the State Department said.

The department’s spokeswoman, Morgan Ortagus, said in a statement that Mr. Pompeo aimed to “ensure the safety and security of Americans in the Middle East” by staying in Washington and would travel in the “near future” to the countries he had been scheduled to visit.

The Iraqi protesters, who were mostly members of Iranian-backed militias, broke into the embassy compound on Tuesday and set some outbuildings on fire. The attackers trapped diplomats and other embassy employees inside larger buildings, but the ambassador, Matthew Tueller, was outside the country on leave. The protests on Wednesday were calmer, and no demonstrators breached the gates. Protesters dispersed in the afternoon, and there were no reports of injuries.

Former State Department officials and associates of Mr. Pompeo say he has been keen to ensure that American diplomats are not harmed under his watch, especially because as a congressman, he was among the most scathing critics of Secretary of State Hillary Clinton’s handling of a militant group’s attack on an American compound in Benghazi, Libya. The 2012 assault resulted in the deaths of four Americans, including Ambassador J. Christopher Stevens.