* 45-minute gunbattle erupts during attempted escape
* Inmates serving terms on terrorism charges
* Unclear if prisoners had outside help
* Escaped prisoners recaptured alive (Recasts with government calling violence a failed prison break)
By Abdoulaye Massalatchi
NIAMEY, June 1 (Reuters) - Two prison guards were killed and three more were wounded in Niger's capital Niamey on Saturday during what government officials said was a failed escape attempt by Islamist militants serving terms on terrorism charges.
The daytime gunbattle in the city centre comes just a week after al Qaeda-linked groups raided a uranium mine and an army barracks, raising fears that the conflict in neighbouring Mali could spread to other West African states.
Local residents said shooting began at around 3 p.m. (1500 GMT) near the entrance to the prison, which houses over 600 inmates including Nigerian Boko Haram insurgents and members of other armed Islamist groups.
"Three inmates convicted of conspiracy to commit terrorism attempted a mass escape. They attacked the prison guards who returned fire. Two guards were killed and three were wounded," state prosecutor Moussa Waziri told journalists.
A government spokesman said all three of the inmates had been captured alive.
"The situation is now under control," Morou Amadou said.
Neither official would give further details on the inmates' identities or explain how they had come to be armed.
Officials had said earlier that security forces were looking for part of a group who had attacked the prison, believed to have fled aboard a truck, while other gunmen were thought to still be hiding out in houses near the prison.
Local witnesses reported a number of civilians had also been lightly wounded and said they saw several men, their heads covered in turbans, open fire on the guards. They also said they heard a loud explosion.
"We were sitting there when we saw these armed men start to shoot at the guards ... I saw several (guards) fall and not get back up," Ila Yaye, who lives near the prison, told Reuters.
A senior official with the National Guard, which provides the guards for Niger's prisons, said the three escapees included two Boko Haram militants and a Sudanese arms dealer, who had been arrested separately over the last two years.
Witnesses said gendarmes had rushed to the prison to reinforce guards who remained under fire for around 45 minutes. Local residents fled on foot as police blocked off roads leading to the prison, allowing only ambulances into the area.
A prison official, speaking on condition of anonymity, said guards would only take a head count on Sunday morning to ensure that no other prisoners were missing, believing it was too dangerous to carry out the count during the night.
Niger has emerged as a firm ally of France and the United States in the fight against al Qaeda-linked groups in the arid Sahel region.
Its army has deployed 650 troops in neighbouring Mali to take part in a French-led war on armed Islamists groups who seized the northern two-thirds of the country last year.
The government has sought to shut its porous desert borders to Islamist groups that are thought to have shifted their bases to southern Libya and has allowed the U.S. to establish a drone base on its territory.
Niger's soldiers are also participating in joint operations with neighbours Nigeria against that country's Boko Haram militants, whose armed insurgency and bombing campaign has pushed to government to launch a major military offensive.
Mokhtar Belmokhtar, a veteran of al Qaeda's North African operations, claimed that his fighters were behind attacks on an army barracks in Agadez and a mine operated by French company Areva in the remote town of Arlit on May 23.
He said the raids, a joint operation with the MUJWA militant group which killed 24 soldiers and one civilian, had been launched in retaliation for Niger's role in a French-led war on armed Islamists groups in neighbouring Mali. (Reporting by Abdoulaye Massalatchi; Writing by Joe Bavier; editing by Patrick Graham)