The Kurdish leader, Mullah Krekar, has been a refugee in Norway since 1991 and challenged the order to strip him of his refugee status and deport him.
In the 64-page ruling, the Oslo City Court rejected his appeal on Thursday and said it found the grounds for his expulsion to be legitimate.
"The resolution about expulsion due to national security is valid," said the court, which gave Krekar a one-month deadline to appeal.
The court ruling was dated Tuesday, but was not released
Krekar's lawyer, Brynar Meling, said he plans to appeal, in a legal battle that could take years to resolve.
In May, the government upheld a 2003 order by immigration authorities to revoke his residency and deport him because "Krekar represents a danger to national security."
Krekar has said he no longer leads Ansar al-Islam, and denies links to al-Qaida.
Neither Krekar nor the Norwegian government immediately commented on the ruling.
Born Najm al-Din Faraj Ahmad, Krekar founded the group listed as a terrorist organisation by the US and others.
Ansar al-Islam is also suspected in bombings against US-led forces in Iraq.
"The resolution about expulsion due to national security is valid"
Oslo City Court
Krekar has been in and out of police custody during investigations on charges that included financing terror and plotting to kill political rivals in northern Iraq.
However, Norwegian police dropped all charges against Krekar in June 2004, citing lack of evidence and fears that witness testimony in Iraq was coerced.
Krekar was arrested at an airport outside Amsterdam, Netherlands, in September 2002, after Iran denied him entry and sent him back to Europe.
He was deported to Norway in January 2003, and interrogated by intelligence services in both countries.