Taliban fighters and their supporters rallied across Afghanistan to celebrate the end of 20 years of foreign military presence on Tuesday, pledging to implement strict Islamic rule as ordinary Afghans, many of them bewildered and scared, grappled with the uncertain future ahead.

Tens of thousands of Afghans still desperately sought to leave the country by overland routes, and many—particularly in Kabul—viewed the new era in Afghanistan’s bloody history with fear and apprehension.

Senior figures in the Islamist movement posed in front of a C-130 transport plane at the Kabul airport, the hub of a chaotic U.S.-led evacuation operation in recent days. Fighters took selfies in the cockpits of Afghan military helicopters that had been disabled and abandoned.

Taliban members sat in an abandoned Afghan Air Force aircraft at Kabul’s airport on Tuesday.

Photo: wakil kohsar/Agence France-Presse/Getty Images

“We hope that Afghanistan will not be invaded again, that it will be rebuilt, remain independent, and that a holy Islamic system will rule,” Taliban spokesman Zabiullah Mujahid said, as uniformed fighters in modern combat gear knelt on the tarmac and chanted “Allahu akbar,” or “God is great.”

Speaking to members of the Taliban’s Badri 313 unit, Mr. Mujahid said: “I praise all your sacrifices, congratulate you all on the great victory, and on achieving independence and freedom for Afghanistan.”

The day represented a historic moment for the Taliban, who were ousted by the 2001 U.S. invasion in the wake of the Sept. 11 terrorist attacks—plotted by al Qaeda on Afghan soil.

The group has already received congratulations for its comeback from scores of Islamist movements around the world, including those that Washington classifies as terrorist.

Taliban spokesman Zabiullah Mujahid addressed the media at the airport.

Photo: wakil kohsar/Agence France-Presse/Getty Images

While the Taliban offered amnesty to former government officials and soldiers after taking Kabul and tried to project a more moderate image, they are increasingly returning to their old ways. The Taliban-appointed acting minister of higher education has said women and men could no longer attend the same university lectures, women presenters have been banned from Kandahar’s radio and TV, and dozens, if not hundreds, of former security officials have been executed.

The Taliban, who committed to seeking a peaceful settlement in the February 2020 agreement they signed with the Trump administration in Doha, Qatar, managed to seize power on Aug. 15 even though foreign forces still remained in the country.

For the past two weeks, Taliban units deployed in an uneasy cooperation with the U.S. troops around the Kabul airport as Washington and its allies carried out a massive airlift that flew out some 122,000 foreign citizens and Afghans who feared persecution by the country’s new rulers.

Many others, including between 100 and 200 American citizens, have been left behind. There are currently no commercial flight operations in Kabul, and it was unclear when they would resume.

No longer an insurgent movement, the Taliban now must run a country of nearly 40 million people that is cut off from the rest of the world and lacks an internationally recognized government or access to the global financial system and aid.

U.S. Army Maj. Gen. Chris Donahue boarded a transport plane as the last American soldier to leave Kabul, according to the XVIII Airborne Corps.

Photo: us army/Reuters

The Taliban also have to contend with the threat posed by the more radical Islamic State, which carried out last week one of the deadliest terrorist attacks of the war, killing 13 U.S. troops and some 200 Afghans.

Since the fall of the Afghan republic, prices of basic commodities have soared, the Afghan currency plummeted and the banking system seized up, with withdrawals limited to $200 a week and long lines forming outside branches. Kabul’s main money-changers market, Saray-e-Shahzada, remains closed.

The U.S. and other Western nations as well as India have shut their embassies in Kabul. Pakistan, China, Russia, Iran and NATO member Turkey, however, have retained diplomatic presence in the Afghan capital. While not formally recognizing the Taliban authorities, they are in regular contact with them.

Senior Taliban officials have said that they seek an inclusive government and in recent weeks met several key figures of the deposed Afghan republic, such as former President Hamid Karzai and former chief peace negotiator Abdullah Abdullah.

These, however, were mostly courtesy visits during which little substance was discussed, a person familiar with these meetings said. Mr. Karzai, in particular, has been advised by the Taliban not to leave his residence for safety reasons, essentially placing him under a soft form of house arrest, the person said.

In Kabul, a city of some six million people, many were scared by nighttime celebratory gunfire that followed the American exit, and were ill at ease with the Taliban’s rigid new rules.

Taliban supporters celebrated in Kabul after the last U.S. aircraft took off.

Photo: -/Agence France-Presse/Getty Images

“The way the Taliban treat people is very rude,” said one 28-year-old resident who used to import groceries from Iran but now is jobless. “No one can raise their voice because everyone is afraid of them.”

For Taliban fighters and their supporters across Afghanistan, Tuesday was a day of joy. A Toyota Hilux drove around the eastern city of Khost with mock coffins draped in the flags of the U.S., the North Atlantic Treaty Organization, the U.K. and France, according to footage posted on social media.

“The cadavers have left Afghanistan forever, we congratulate you on this big victory,” said the banner on the hood. Later, the coffins were paraded through a large victory rally in the city center.

To the approving roar of thousands, a senior Taliban leader in Khost praised the suicide bombers who he said drove the American invaders away. “We will establish the Islamic system you yearned for,” he said, according to a recording of the rally. “Your dreams have been realized. We have achieved the dream for which you had blown up your flesh. We congratulate you in your graves.”

Large crowds also gathered in Kandahar, the Islamist movement’s birthplace, waving the Taliban’s white flags as passing cars and auto rickshaws honked in support, according to videos posted on social media. Taliban fighters on motorcycles paraded through the streets.

The Taliban now control all of Afghanistan’s 34 provinces except for one: Panjshir, a narrow valley north of Kabul where anti-Taliban militias and some remnants of the defeated Afghan army remain holed up. Though the Taliban and the Panjshiris are negotiating a possible deal, armed skirmishes continue at the entrance to the valley.

Fahim Dashty, an aide to the Panjshiri resistance leader Ahmad Massoud, said several Taliban members were killed overnight. “They wanted to test their luck by attacking Panjshir,” he said. “But by God’s mercy, they were not lucky.”

Write to Yaroslav Trofimov at yaroslav.trofimov@wsj.com