KABUL, Afghanistan — The Taliban assailants apparently thought they were attacking an unprotected Christian-run day-care center. But they mistakenly burst into the compound next door, where a U.S. government contractor’s employees were heavily armed and ready, according to accounts that the contractor and the Afghan police gave Friday of a wild four-hour shootout in Kabul.

The contractor, Roots of Peace, which runs agricultural projects financed by the U.S. Agency for International Development, had taken the precaution of blocking its front gate with an armored Land Cruiser, which guards used to take cover behind and shoot at the attackers, said Gary Kuhn, the group’s president, interviewed by telephone from its headquarters in San Rafael, Calif.

That slowed the attackers enough for the guards and the five foreign residents to retreat into the house and upstairs. “There’s a circular staircase which is very hard to take cover on. One tried coming up it, and the guard shot him,” Kuhn said, citing accounts from his staff in Kabul.

Two of the residents, Americans, hid in their bedroom closets. “One very big tall man hid in a closet and piled clothes on top of himself, while the Taliban were shooting in his room, throwing flash grenades, and even opened the closet door but didn’t see him,” said Heidi Kuhn, the group’s chief executive and Kuhn’s wife, who also was interviewed by telephone in California. “It’s a miracle all of them escaped.”

Unlimited Digital Access. $1 for 4 weeks.

Gary Kuhn said the bedroom shooter might have been a police officer clearing the scene, since the Taliban assailants were unlikely to have used flash grenades, which are designed to stun and frighten, not kill.

While the gunbattle was under way, next door, at what apparently had been the Taliban’s intended target, a Christian-run day-care center that had no armed guards and normally left its front door open, police rescued two dozen foreigners, according to Gen. Mohammad Ayub Salangi, the deputy interior minister, who went to the scene. Their nationalities were unclear, but they appeared to be Americans or Europeans.

Journalists who saw them escorted out said at least five young Western children were among the group, highly unusual sights here.

Kuhn, who with his wife has visited Kabul frequently, said the day-care center was not a church, as the Taliban had asserted, but acknowledged that “They do have religious services there on certain days.”

Expatriates in Kabul dropped children off there for the day routinely, he said. Nevertheless, very few organizations employing expatriates in Afghanistan allow them to bring families and children because of the risk.

Afghan officials said all five Taliban attackers were killed, including one who committed suicide. The death toll also included two Afghan civilians, one of them a young girl. Two Roots of Peace guards were wounded.

The shootout was the latest in a series of deadly attacks on foreign journalists, aid workers and visitors since January, amid the heavy security for the upcoming Afghan presidential-election campaign.

The attack came less than a week after suicidal gunmen invaded the luxury Serena Hotel, killing nine people, including a prominent journalist and most of his family.

Also this month, a Swedish journalist was killed on a street in the diplomatic quarter by insurgents who accused him of spying. In January, an attack on a restaurant killed 13 foreigners, many of them relief workers and employees of international organizations.

Friday’s attack took place in Kabul’s well-to-do Karte Seh area where many foreign-aid groups have offices and homes. Afghanistan has more than 2,000 registered nongovernmental organizations, most with offices in the capital. Most live in unguarded or lightly guarded guesthouses or individual homes, but those with government contracts, like Roots of Peace, often employ guards.

The residents in the group’s house included the two Americans and an Australian, a Malaysian and a South African, who was the group’s security adviser. The group runs agricultural and demining programs in Afghanistan, as well as in Israel, Croatia and Vietnam.

A spokesman for the Taliban, Zabiullah Mujahid, reached by telephone while the attack was under way, said the target had been “a church used to convert Muslims to Christianity.”

Mujahid said the Taliban had intelligence that the suspected underground church was celebrating its anniversary in Kabul, and the insurgents timed their attack to what they thought was that celebration.

There are no known churches in Afghanistan, where the practice of Christianity is outlawed and conversion of Muslims is a crime. Aid groups supported by Christian groups abroad have in the past been accused of being missionaries, and two were expelled by Afghanistan in 2010.