Oil prices firmed on Oct. 17, building on gains made as fighting between Iraqi and Kurdish forces threatened supplies from northern Iraq while tension rose between the U.S. and Iran.

After months of rangebound trading, during which OPEC-led supply cuts supported crude but rising U.S. output capped markets, prices have made significant gains this month.

Brent crude advanced by 20 cents to $58.02 a barrel (bbl) by 6:35 a.m. CST (11:35 GMT), up by about a third from its mid-year levels. U.S. West Texas Intermediate light crude was up 20 cents at $52.07.

The Baghdad government recaptured territory across northern Iraq from Kurds on Oct. 17, widening a campaign that has shifted the balance of power in the country.

The fighting in one of Iraq's main oil-producing areas helped to restore a risk premium on oil prices, though officials said that oil fields in the region were operating normally.

RELATED: Iraq Warns Kurdistan Not To Shut Down Kirkuk Oil Flows

"The latest bout of geopolitical premium to strike the energy complex remains alive and well as oil prices build on recent gains," said Stephen Brennock, analyst at London brokerage PVM Oil Associates.

Goldman Sachs said the Kirkuk cluster of oil fields, which pump about 500,000 bbl/d and extend from the autonomous Kurdistan region into northern Iraq, were at risk from the conflict.

Tension between the United States and Iran is also rising, increasing the global risk premium for oil.

U.S. President Donald Trump on Oct. 13 refused to certify Iran's compliance over a nuclear deal, leaving Congress 60 days to decide on further action against Tehran.

"If there [were new sanctions], we expect that several hundred thousand barrels of Iranian exports would be immediately at risk," Goldman said.

During the previous round of sanctions against Iran, about 1 million bbl/dd of oil was cut from global markets.

"Oil and geopolitics are very much interlinked," Fatih Birol, executive director of the International Energy Agency, told Reuters. "Oil security remains a critical issue."

With supply cuts led by the Organization of the Petroleum Exporting Countries tightening the market, analysts have been raising their oil price forecasts.

Birol said the rate of compliance by OPEC and its partners in their targeted cutting of about 1.8 million bbl/d between January this year and March 2018 was about 86%.

Bank of America Merrill Lynch said it was raising its oil price forecasts.

"We see Brent averaging $54 this quarter and $52.50/bbl in first-half 2018, compared with our previous forecasts of $50 and $49.50/bbl respectively," it said.