WASHINGTON — Foreign terrorist groups and their affiliates had a bad year in 2017 as the United States and other countries fought back against the Islamic State, but Al Qaeda and Iranian-backed militias remain deadly threats, according to an annual government terrorism report that was released on Wednesday.

There were 8,584 terrorist attacks around the world in 2017, a 23 percent decline from 2016, according to the State Department report. As a result, more than 18,700 people were killed, about a quarter of whom were the perpetrators themselves.

That death toll represented a 27 percent drop from the previous year, the report said.

Much of the reason for the decline was the improved security situation in Iraq, according to Ambassador Nathan Sales, the State Department’s coordinator for counterterrorism.

Still, more than half of all terrorist attacks worldwide took place in just five countries: Afghanistan, India, Iraq, Pakistan and the Philippines. And 70 percent of all deaths from terrorist attacks occurred in a different, if overlapping, set of five countries: Afghanistan, Iraq, Nigeria, Somalia and Syria.