"The suicide attacker rammed his explosives-laden car into the wall of the German consulate in the city," said local police chief Sayed Kamal Sadat.
The attack left several people dead and more than 120 people injured according to Afghan officials. All German employees at the consulate are “safe and unharmed”, according to the German Foreign Ministry.
A third man also riding a motorbike was seriously injured. Spiegel's sources claim the men failed to react to warning shots.
A suspect had also been detained near the diplomatic mission on Friday morning, said deputy police chief Abdul Razaq Qadri.
The German Foreign Ministry said in a statement released overnight that "several heavily armed men" were driven back by German soldiers and Afghan special forces after the attack.
The Taliban called it a "revenge attack" for a US airstrike in the volatile province of Kunduz earlier this month that left up to 32 civilians dead.
A spokesman for the Taliban told DPA that Germany had been involved in the deadly US airstrike on the November 3rd by providing the necessary intelligence information.
“Why shouldn’t we attack the Germans? Germany was directly involved in the airstrike which cost civilian life. The attack was based on intelligence that German soldiers had given the American troops,” the Taliban spokesperson said.
Sporadic gunfire rattled the usually tranquil city after the huge explosion on Thursday evening, which smashed windows of nearby shops and left terrified local residents fleeing for cover.
Afghan special forces cordoned off the area as helicopters were seen flying over the consulate and ambulances with wailing sirens rushed to the area.
Six dead bodies and more than 120 wounded people - including at least ten children - had been brought to two local city hospitals as of Friday morning, said Noor Mohammad Fayez, the head doctor. Some of the wounded are in a critical condition, Fayez said.
Police reported that one insurgent had blown himself up in the attack.
Local journalist Bilal Sarwary reported that the two dozen Germans working inside the consulate were taken to a German military base which is situated around 10km away.
A resident of the area, Mirza Mohammad, told DPA that a few minutes after the explosion, he heard gunshots and someone yelling "allahu akbar" (God is the greatest) before everything turned silent.
Local journalist Tahir Qadiry tweeted on Thursday evening that German soldiers fired shots in the air to stop anyone approaching the compound.
A Bild journalist also tweeted what appears to be the Taliban's claim of responsibility along with a photo of the compound.
The strikes killed several children, after a Taliban assault left two American soldiers and three Afghan special forces soldiers dead near Kunduz city.
The UN said it would investigate the strike.
The UN's envoy to Afghanistan Tadamichi Yamamoto said after the incident that civilian casualties were unacceptable and undermined efforts to establish peace and stability in the country.
The carnage underscores worsening insecurity in Afghanistan as Taliban insurgents ramp up nationwide attacks despite repeated government attempts to jumpstart stalled peace negotiations.
Taliban spokesman Zabihullah Mujahid said the "martyrdom attack" on the German consulate had left "tens of invaders" dead. The insurgents are routinely known to exaggerate battlefield claims.
Civilian casualties caused by NATO forces have been one of the most contentious issues in the 15-year campaign against the insurgents, prompting strong public and government criticism.
Afghanistan's worsening conflict has prompted US forces to step up airstrikes to support their struggling Afghan counterparts, fuelling the perception that they are increasingly being drawn back into the conflict.
The latest attack in Mazar-i-Sharif comes just two days after a bitter US presidential election.
Afghanistan got scarcely a passing mention in the election campaign - even though the situation there will be an urgent matter for the new president.
President-elect Donald Trump is set to inherit America's longest war with no end in sight.