A Taliban suicide bomber rammed a truck packed with explosives into a wall around the German consulate in the northern Afghan city of Mazar-i-Sharif, killing at least four civilians and wounding scores, officials said.

The attack highlighted the security problems spreading across Afghanistan in recent months, underlining one of the most intractable foreign policy challenges that will face U.S. president-elect Donald Trump when he takes office next year.

A NATO spokesman said the explosion late on Thursday had caused "massive damage" to the building, shattering windows as much as five kilometres away. Heavily armed attackers followed up the initial blast, battling with Afghan and German security forces late into the night before the attack was suppressed.

"The extent of damage to the city is huge," said Abdul Razaq Qaderi, deputy police chief of Balkh province. "This kind of an attack, bringing a truck full of explosives and blowing it up in the city, had never happened before.

"The city is still recovering from the shock."

Noor Mohammad Faiz, the head doctor in Mazar-i-Sharif provincial hospital, said four bodies and 120 wounded, most hurt by flying glass, had been brought to the hospital. Qaderi said German troops had also shot two men on motorcycles who did not comply with orders to stop.

A foreign ministry spokesman in Berlin said all German employees of the consulate general were safe and uninjured.

The Taliban claimed responsibility for the attack, saying it was in retaliation for NATO air strikes against a village near the northern city of Kunduz last week in which more than 30 people were killed.

The Islamist movement's spokesman, Zabihullah Mujahid, said by telephone that heavily armed fighters, including suicide bombers, had been sent "with a mission to destroy the German consulate general and kill whoever they found there".

More than 30 people, many of them children, were killed last week when U.S. aircraft carried out air strikes in support of Afghan and U.S. special forces who came under attack during a raid against suspected Taliban militants threatening Kunduz. Two U.S. servicemen were killed.

Germany, which heads the NATO-led Resolute Support mission in northern Afghanistan, has about 850 soldiers at a base on the outskirts of Mazar-i-Sharif, with another 1,000 troops coming from 20 partner countries.

Afghan security forces investigate at the site of explosion near the German consulate office in Mazar-i-Sharif, Afghanistan on Friday. (Anil Usyan/Reuters)

Shattered windows

The explosion occurred about an hour before midnight local time, a spokesman for the German military joint forces command in Potsdam said.

Witnesses reported sporadic gunfire from around the consulate and said the huge blast had shattered windows in a wide area around the compound.

"It was a prepared attack for which we made all arrangements," Mujahid said. "As per our plan, first a suicide bomber driving an explosives-laden vehicle rammed the main building of the consulate and that enabled other fighters to move in and kill all the foreigners there."

He said dozens of German soldiers and intelligence personnel were killed in the attack although the Taliban often exaggerate casualties caused by its operations.

Suspect arrested

After Afghan special forces, German security personnel and NATO's quick reaction protection force intervened, fighting was over by the early hours of the morning, said Sayed Kamal Sadat, police chief of Balkh.

At least one suspect was arrested from the area of explosion, officials said.

The NATO spokesman said at least one car packed with explosives had been rammed into the high outer wall surrounding the consulate, but authorities were investigating if a second car had been involved.

The heavily protected consulate is in a large building close to the Blue Mosque in the centre of Mazar-i-Sharif, where the Indian consulate was also attacked by militant gunmen earlier this year.

A crisis task force was set up in Berlin and Foreign Minister Frank-Walter Steinmeier was receiving continual updates, the German foreign ministry spokesman said.