* U.S. defense secretary safe, nowhere near explosion
* Nine civilians killed, 14 people wounded in Kabul
* Suicide attack in Khost kills eight
* Contentious Bagram prison transfer falls through (Adds prison transfer postponed)
By Mirwais Harooni and Phil Stewart
KABUL, March 9 (Reuters) - A suicide bomber blew himself up at a Defence Ministry gate in Kabul on Saturday killing nine civilians during a visit to Afghanistan by U.S. Defense Secretary Chuck Hagel, underlining the insurgent threat as NATO troops prepare to leave.
Hagel was nowhere near the explosion, said a spokesman for Afghanistan's NATO-led International Security Assistance Force (ISAF). A U.S. defence official said Hagel was in a safe location at an ISAF facility.
An Afghan Defence Ministry official said at least nine people, all civilians, were killed. Fourteen people were wounded in the attack, carried out by an insurgent on a bicycle.
Roads around the ministry building, which is near the presidential palace, were closed as emergency officials cleared the area of debris and washed blood from the street. A wall surrounding the ministry was pockmarked with shrapnel.
The Afghan Taliban claimed responsibility for the attack and said the ministry was the target. They said in a statement the attack "is a kind of message" for Hagel.
The blast underscored the security challenges facing Afghanistan as U.S.-led NATO forces prepare to leave the country by the end of 2014.
Some Afghans fear that another civil war could erupt or the Taliban will make a push to take control of the country again after Western combat troops withdraw.
In the eastern province of Khost, a suicide bomber attacked a joint Afghan and foreign patrol, killing seven civilians and a policeman, the provincial governor's office said in a statement.
After the blast in Kabul, the defense secretary flew to Bagram Air Base near Kabul for meetings with commanders.
Hagel, who arrived in Afghanistan on Friday for his first trip abroad as defense secretary, is due to hold talks with President Hamid Karzai, whose recent orders to curtail U.S. military activity highlights an often tense relationship with the 66,000 American forces here.
Another contentious issue is a U.S.-run prison at the Bagram base, which had been due to be handed over to full Afghan control on Saturday. But a planned ceremony to mark the final transfer of Afghan prisoners fell through at the last minute.
A spokesman for U.S. forces in Afghanistan said the two sides were working out the details of the transfer.
Hagel's visit also coincides with the passing of a deadline imposed by Karzai for U.S. special forces to leave the province of Wardak, after Karzai accused them of overseeing torture and killings in the area.
U.S. forces have denied involvement in any abuses and it was not clear if they were leaving Wardak by the deadline.
Afghans are divided, however, saying the expulsion of the U.S. forces could leave a vacuum for insurgents to fill, which would pose a security risk for nearby Kabul.
Hagel, in Kabul to make his own assessment of America's longest war as it enters its final stretch, is making his first trip to Afghanistan since a mid-2008 visit with then-Senator Barack Obama during Obama's campaign for the presidency.
Obama, a Democrat, forged a close bond with Hagel, a Republican, and remarked later that summer that the two agreed on almost "every item" of foreign policy.
Hagel was confirmed as defense secretary on Feb. 26 and was sworn into office the next day.
His advice may help shape some of Obama's most lasting decisions on Afghanistan, notably how large a residual mission to keep there once NATO wraps up its combat mission at the end of next year and the vast majority of foreign forces go home.
On Tuesday, the outgoing head of the U.S. military's Central Command, General James Mattis, disclosed that he recommended keeping 13,600 American troops in Afghanistan - above the range of troop levels U.S. officials have said were being considered by the White House and discussed by NATO defence chiefs last month. [ID: nL1N0BXGWZ]
Obama last month announced the withdrawal of 34,000 American troops - about half the total - by early next year. Officials also have outlined the expected pace of the withdrawal through next April. (Writing by Michael Georgy; Editing by Jeremy Laurence and Robert Birsel)