(Reuters) - Islamic State militants clashed with security forces inside Iraq's largest refinery on Thursday and held on to recent gains in the west of the country, as Prime Minister Haider al-Abadi said the extremist group remained "very, very dangerous."

The insurgents suffered a major defeat this month when Iraqi troops and Shi'ite paramilitaries routed them from the city of Tikrit, but are now striking back at Baiji refinery and in the western province of Anbar.

Baiji was attacked several days ago by the militants, who blasted their way through the perimeter and took control of several installations, including a distribution point and storage tanks. They have managed to hold those parts of Baiji.

A source in the military operations command for Salahuddin province where Baiji is located said an Iraqi army battalion had arrived to help defend the refinery on Thursday, and the militants had not been able to take any major infrastructure.

The top U.S. military officer, General Martin Dempsey, told reporters "the refinery itself is at no risk right now." But he expressed concern that the militants had penetrated the refinery's outer perimeter and were now inside.

Islamic State sympathizers circulated photographs on social media late on Thursday appearing to show the militants inside the refinery with the caption: "the soldiers of the (Islamic) State advance to cleanse what is left of Baiji refinery".

The images could not be independently verified.

Speaking at the Center for International and Strategic Studies think tank in Washington, Prime Minister Haider al-Abadi said the militants wanted to send a message after losing Tikrit.

"I think it's timed to (coincide with) my visit to the U.S. I think they want to show that despite the support that Iraq is receiving, they are there to cause damage."

During his visit, Abadi outlined plans to prioritize the battles in both Baiji and Anbar, where Islamic State overran an area at the eastern edge of the provincial capital Ramadi on Wednesday.

The government last week announced a new offensive to recapture Anbar, seeking to build on the recent victory in Tikrit, but have since lost ground in the vast desert terrain.

Anbar governor Sohaib al-Rawi speaking on Iraq state TV said "all sons of Iraq" were welcome to participate in liberating the province from Islamic State, indicating no objection to the involvement of Shi'ite paramilitaries.

Dempsey said Baiji was more strategic than Anbar, given Baiji's critical oil infrastructure, and did not appear to rule out the possibility Ramadi might fall, if only temporarily.

"I would much rather that Ramadi not fall, but it won't be the end of the campaign should it fall. We got to get it back," Dempsey, chairman of the Joint Chiefs of Staff, told a Pentagon news conference.

Shi'ite militia have played a leading role in driving back the insurgents after the army disintegrated last summer, but some tribes in Anbar, a Sunni province, have expressed reservations about the involvement of paramilitaries there.

A police source in Anbar said Shi'ite militias had arrived in Ramadi to take part in trying to push back Islamic State.