The militants, armed with rocket propelled grenades and machine guns, disguised themselves as women in burkas to sneak unnoticed into a building that looks onto the heavily fortified Independent Election Commission (IEC) headquarters in Kabul, deputy interior minister Mohammed Ayub Salangi said.
Afghan security forces battled the militants for about five hours, while frightened IEC staff and eight international UN employees took refuge in safe rooms inside the compound.
Three security force members were injured, but there were no reports of any other casualties.
The Taliban claimed responsibility for the assault, the latest in a spring offensive aimed at disrupting presidential elections due to be held on April 5.
Election commission staff heard an initial explosion at around midday yesterday at the IEC headquarters, followed by gunfire and rockets, one of which damaged warehouses inside the compound, according to an IEC employee.
Mohammad Zahir, Kabul's police chief, told reporters that suicide bombers armed with "light and heavy weapons" were shooting at the IEC compound and at passers-by.
The IEC compound is close to the United Nations Office Complex in Afghanistan (UNOCA) and other international organisations.
It is also located near Kabul international airport, which was shut for more than two hours.
"I am here ... The attack is going on around the IEC compound," said commission spokesman Noor Mohammad Noor speaking by telephone from a safe-room inside the building as the attack was under way.
He said that IEC personnel were safe and Afghan security forces were in control of their building.
The deputy of the IEC's media monitoring commission, Ashmat Radfar, who hid in the basement with about 40 other people during the attack, said about 15 rocket-propelled grenades had fallen in the area and two warehouses were hit and set on fire. He said the warehouses did not contain ballots.
UN staff at the complex near the IEC premises were instructed to take refuge in safe-rooms. The owner of a house used to shoot at the IEC building said that three guards were present at the time. "I had three guards, two outside and one inside ... The attackers were wearing women's burkas," said Haji Mohibullah.
His house was being used as an office by campaigners for presidential candidate Gul Agha Sherzai.
Afghans are voting for a successor to president Hamid Karzai, who is constitutionally barred from running for another term in office.
The vote is seen as a major test by foreign donors who are hesitant about bankrolling the government after most Nato troops in Afghanistan withdraw later this year.
The attack occurred less than 24 hours after militants stormed a guesthouse used by a US-based aid group, resulting in the death of an Afghan child.
Last week, nine people including a prominent Afghan journalist were killed in an attack on a highly fortified hotel in the capital.
The Taliban have stepped up attacks on foreigners, suggesting that they are shifting tactics to focus on civilian targets that aren't as heavily protected as military and government installations.