Eight Afghan soldiers were killed on Tuesday while repelling a fierce Taliban attack on Kunduz, a strategic city in northern Afghanistan that had briefly fallen to the militants twice in the past, officials said.

In a separate incident north of the capital Kabul, gunmen opened fire on worshippers offering evening prayers at a mosque, killing at least seven people and wounding 12, police said.

Fighting erupted during the night in Kunduz as Taliban fighters attacked several government posts on the outskirts of the city at around 1:00 am Tuesday, a defence ministry statement said.

"With the support of air force their attack was repelled," it said, adding the fighting lasted for several hours.

Defence Minister Assadullah Khalid, who visited Kunduz later on Tuesday, said the Taliban had "suffered big losses".

"Unfortunately, we have also lost eight brave soldiers," he told reporters.

The insurgents managed to briefly capture an Afghan army post during Tuesday's fighting but it was quickly retaken by security forces, army spokesman Hadi Jamal told AFP.

Three civilians were also killed and 55 others wounded in the overall violence that rocked the city, Kunduz provincial health director Ehsanullah Afzali said.

The strategic city of Kunduz in northern Afghanistan has briefly fallen twice to the Taliban in the past
The strategic city of Kunduz in northern Afghanistan has briefly fallen twice to the Taliban in the past AFP / Bashir KHAN SAFI

An air strike also hit and partially damaged a clinic in the nearby district of Chardara but there were no reports of casualties, he added.

Both the Taliban and Afghan forces have clashed repeatedly in rural areas in recent months, but an attempt to enter a city as large as Kunduz is seen as a serious escalation.

Meanwhile, gunmen stormed a mosque in Khelalzai village in the province of Parwan, north of Kabul, and opened fire on worshippers offering evening prayers.

"Seven people have been killed and 12 wounded. The gunmen have fled the area," Parwan police chief Haroon Mubarez told AFP.

The ministry of interior confirmed the incident but provided a slightly higher death toll, saying eight had been killed.

The latest violence follows a declaration by President Ashraf Ghani last week to resume offensive strikes against the insurgents following a string of brutal attacks.

Afghan security forces patrol Kunduz after a Taliban assault on the city
Afghan security forces patrol Kunduz after a Taliban assault on the city AFP / STR

The Taliban responded by vowing to increase assaults against Afghan security forces.

The defence ministry said 40 Taliban fighters were killed and 50 others wounded in the Kunduz assault.

Afghan officials and the Taliban frequently exaggerate claims of casualties.

The Taliban briefly captured Kunduz, a strategic city not far from the Tajikistan border, twice before -- in September 2015 and again a year later. An attempt last year failed.

During the fight for the city in 2015 US air strikes destroyed a Doctors Without Borders hospital in Kunduz, killing 42 people.

The latest attack on the city came a day after the Taliban killed at least seven Afghan intelligence personnel with a car bomb in the eastern province of Ghazni.

And last week, gunmen stormed a maternity hospital in Kabul killing 24, including mothers and infants.

That attack, which triggered international outrage, was followed by a suicide bombing at a funeral which killed at least 32 mourners.

The Taliban denied involvement in those attacks, although Ghani blamed the militants and the Islamic State group for the bloodshed.

Officials say the assaults are part of the Taliban's undeclared spring offensive.

Violence has spiked since the Taliban signed a deal with Washington in February, under which Washington said it would pull its troops out of the country in return for security guarantees from the militants.

Analysts say the Taliban have been emboldened by the accord and government officials have reported more than 3,800 attacks, which killed 420 civilians and wounded 906, since signing the deal.

The Taliban were responsible for 208 civilian casualties last month -- 25 percent more than April 2019 -- the United Nations Assistance Mission in Afghanistan (UNAMA) said in a statement.

It said civilian casualties attributed to Afghan security forces in April numbered 172, an increase of 38 percent.

"Parties have committed to finding a peaceful solution and should protect the lives of all Afghans and not jeopardise people's hope for an end to the war," said Deborah Lyons, the UN Secretary-General's Special Representative for Afghanistan.