A soldier searches an Afghan while standing guard Tuesday in Badakhshan province, where security has been intensified after weekend attacks that killed 18 Afghan soldiers. (Muhammad Sharif/European Pressphoto Agency)

— Hundreds of foreign militants are fleeing a months-long Pakistani military offensive and seeking sanctuary in Afghanistan, bolstering the ranks of Taliban factions and triggering one of the bloodiest starts to the spring fighting season in years, according to Afghan officials and analysts.

The growing influence of the foreign fighters, officials said, was evident over the weekend in the northeastern province of Badakhshan, where a few hundred Taliban fighters overran Afghan army posts, killing 18 soldiers and wounding 10. Eight of the soldiers were beheaded — a first by the Taliban in this region — before the insurgents vanished into the mountains with seized weapons.

“The foreign Taliban fighters beheaded the soldiers, not the local Taliban,” said Ahmad Nawid Froutan, a spokesman for the provincial governor. “In the past, the local Taliban have never committed such brutality.”

The fresh wave of foreigners, with probable ties to al-Qaeda and the ultra-violent Pakistani branch of the Taliban, has added a potential new dimension to the Afghan conflict, threatening more instability in the first year after the United States officially declared its longest war over. With most American and international forces gone, the foreign fighters will further test the already beleaguered Afghan security forces as they battle to fill the military gap.

In the political realm, the cross-border influx is triggering anger among Afghan lawmakers and power brokers, potentially tainting President Ashraf Ghani’s ongoing efforts to improve relations with neighboring Pakistan and gain its assistance in facilitating peace talks with the Taliban.

Afghans have long accused Pakistan of meddling in their affairs and of using their military and intelligence services to back the Taliban. Even with Ghani’s overtures, suspicion of Pakistan remains deep. Both Kabul and Washington have been pressing Islamabad to target the militant havens in its northwestern tribal regions that border Afghanistan.

On Tuesday, the anger was palpable in the Afghan parliament. In a televised session, lawmakers railed at top police and military commanders for their failure to prevent the Taliban from brutally targeting their soldiers. Some thought that the foreign fighters were aligned with the Islamic State, also known as Daesh, underscoring a prevailing fear here of the militant group, whose base is in Syria and Iraq.

“Taliban and Daesh haven’t got fighter jets or choppers; they haven’t got heavy artillery and bulletproof military trucks,” declared one lawmaker. “But they are still able to attack Afghan security forces and behead them. It clearly shows that you guys are not doing your duties properly, and so you should resign.”

Their outcry came minutes after Interior Minister Noor-ul-Haq Ulumi informed them that 11 Afghan provinces face high security threats, and an additional nine face medium-level threats. He placed the blame on Pakistan’s offensive in its North Waziristan region that he said was pushing insurgents to enter Afghanistan. “The foreigners are behind the recent insecurity in the country,” Ulumi told the lawmakers.

In June 2014, the Pakistanis launched their long-promised military offensive in North Waziristan and other border zones. The region is part of the tribal areas where the leadership of the Taliban and al-Qaeda sought sanctuary after the U.S.-led intervention in 2001 that toppled the Taliban regime after the Sept. 11 terrorist attacks.

In North Waziristan, tribesmen said the Pakistani operation has driven most militants from major population centers in the area. Still, they say, considerable danger remains along the immediate Pakistan­-Afghanistan border because many of the militants have shifted into adjoining areas of Afghanistan.

Gen. John F. Campbell, the top U.S. commander in Afghanistan, has publicly urged both countries’ forces to work closer together along the border areas.

Map: Flow of foreign fighters to Syria

A Pakistani intelligence officer, who spoke on the condition of anonymity in order to talk freely about the matter, conceded that part of the threat had migrated into Afghanistan. But the officer said Pakistani and Afghan officials have dramatically increased their communication and cooperation in a bid to employ a joint strategy against the militants.

“We have cleared our areas and pushed them out of Waziristan,” the officer said. “We have improved our cooperation and also sharing intelligence with” the Afghans.

The new militants arrive as insecurity has reached unprecedented levels. Last year was the deadliest on record for Afghan soldiers and civilians. This year already shows signs of becoming bloodier: In the first quarter, civilian casualties from ground battles were 8 percent higher than during the same period in 2014, according to a report released Sunday by the U.N. mission in Afghanistan.

“Considering the influx of foreign fighters in Afghanistan, this fighting season will be the bloodiest fighting season,” predicted Atiqullah Amarkhel, a military analyst in Kabul.

In Badakhshan, the local Taliban are mostly ethnic Tajiks, the same ethnic group as the population. In December, the faction released 24 captured police officers — all unharmed. At the time, one survivor told a Washington Post reporter that the local Taliban stopped a small group of foreign fighters who wanted to behead them.

Since December, more foreign fighters have flowed into the province, many via Pakistan, local officials said. A considerable number have brought their wives and children. They include fighters from Pakistan, Chechnya, Tajikistan and Uzbekistan, said the officials, adding that the remote, rugged province, with its large swaths of ungoverned areas, is an ideal haven for the militants.

“They are trying to increase the distance between the local Taliban and the Afghan government,” said Gen. Nazir Mohammed Nayazee, the mayor of Faizabad, the provincial capital. “They want to block efforts at reconciliation and to create a permanent base for themselves.”

The foreigners at first started to train local Taliban fighters in making roadside bombs. But as their numbers have grown, they have grown more influential. In Pakistan, there have been a few beheadings of soldiers. In at least two Afghan areas, the Kostag Valley and the Ragestan district, the foreigners have taken over the leadership from the local Taliban, said Shah Waliullah Adeeb, the provincial governor.

“They will make the local insurgency stronger,” he said. “And as a result, it will destabilize the country more this year.”

Tim Craig and Haq Nawaz Khan contributed to this report from Pakistan. Mohammad Sharif contributed from Kabul.

Read more:

The Islamic State’s atrocities

Islamic State appears to be fraying from within

Foreign fighters flow to Syria

Today's coverage from Post correspondents around the world