Taliban gunmen stormed a Kabul guest house used by a U.S.-based aid group and held four foreigners hostage for several hours on Friday, just eight days before Afghanistan holds a presidential election which the militant group has vowed to derail.

The siege of the walled compound, which is also home to a small church, ended after Afghan security forces killed the last Taliban gunman holed up inside, a military commander said.

At least one Afghan child was killed when a suicide bomber blew himself up outside the building and insurgents forced their way in. There were no casualties among foreigners.

"The fight is over. Five attackers are dead," Qadam Shah Shaheem, commander of 111 Military Corps Kabul, told Reuters.

"One detonated his car loaded with explosives, three others detonated explosives attached to their bodies inside the building, and one was shot by security forces. All four foreigners are alive and safe now."

Kabul is already on high alert and people across the country are on edge ahead of the April 5 vote, which the Taliban have denounced as a Western-backed sham. 

Earlier, a Reuters witness saw about 20 people who appeared to be non-Afghans being evacuated from the guest house in an upmarket residential area of Kabul, many looking frightened and shocked.

A gun battle erupted after a Taliban suicide bomber blew himself up outside the building and insurgents forced their way in. Heavy gunfire resonated in the area for hours after the initial explosion.

Kabul police chief Mohammad Zahir said during the firefight that three U.S. citizens, a Peruvian, a Malaysian and one African had been trapped inside, but the country manager of an organization using the guest house said only four people were trapped.

Taliban on the attack

The Taliban have ordered their fighters to go all out to disrupt the election and threatened to kill anyone who takes part.

Violence has spiralled in Afghanistan in recent weeks, with Taliban suicide bombers and gunmen attacking an election commission office in Kabul on Tuesday.

Afghanistan

An Afghan man helps an injured man at the site of an attack by Taliban insurgents in Kabul on Friday. A group of Afghan Taliban insurgents forced their way into a guesthouse used by foreigners in an upscale residential part of the capital. (Omar Sobhani/Reuters)

Last week, nine people including an AFP journalist and an election observer were killed in an attack on a highly fortified hotel in Kabul.

A senior police official said one attacker in Friday's raid was still resisting, with 18 foreigners, including children, having been evacuated and taken to a safe house.

Deputy interior minister, General Ayoub Salangi, told Reuters that at least one Australian citizen was among those rescued, but the nationalities of the others were not clear.

A senior police official said the target appeared to be a USAID-funded Afghan non-governmental organization.

The Taliban immediately claimed responsibility, saying in a statement the target was a foreign guesthouse and a church.

Reuters television footage showed Afghan forces sealing off the street in the south of the capital and military convoys rushing to secure the area.

The nation of 30 million is holding an election to choose a successor to outgoing President Hamid Karzai.

It will be a major test for foreign donors hesitant about bankrolling the government after the bulk of NATO troops stationed in Afghanistan withdraw this year.