DOHA, Qatar — When the United States was forced to relocate its diplomatic mission from Afghanistan on Monday, it chose Qatar, a longtime mediator between the West and the Taliban and more recently the only government viewed as capable of coaxing the Afghan militants to stay engaged with the world.

Qatar’s relationship with the Taliban stretches back years, reflecting the affinity of the small Persian Gulf state with Islamists and its wish to be a regional power broker. Its unique position has paid huge dividends in the last few weeks as Qatar played a pivotal role in helping the United States and other countries extricate tens of thousands of their citizens and allies from Afghanistan after the rapid Taliban advance.

Qatar’s connections with the Taliban have made Doha the ­go-to contact for countries seeking influence in Afghanistan, as well as for desperate Afghans trying to leave and news media and nongovernmental organizations seeking help with evacuations.

But Qatar also runs the risk of tarnishing its reputation if it is seen as too quick to embrace a brutal Taliban government, analysts said. “Qatar is happy to be important again, but it is also worried about the optics,” said Cinzia Bianco of the Berlin-based European Council on Foreign Relations.

Qatari officials, in published comments, have urged the Taliban to form an inclusive government, drawing on other Afghan parties, while also prodding foreign governments to be willing to cooperate with Afghanistan’s new rulers.

Speaking during a press appearance Tuesday, Qatari Foreign Minister Mohammed bin Abdulrahman Al-Thani said, “Without engagement, we cannot reach . . . real progress on the security front or on the socioeconomic front.” He added, “If we are starting to put conditions and stopping this engagement, we are going to leave a vacuum, and the question is, who is going to fill this vacuum?”

But he said granting the Taliban formal recognition was not a priority, according to a copy of remarks released by his office.

Most immediately, Qatar is pressing the Taliban to compromise in a disagreement over who will operate and secure Kabul’s international airport. With Qatar’s support, the Turkish government has offered to run the airport, a move that would provide a measure of international legitimacy to the Taliban and reconnect Afghanistan to the outside world. A stumbling block in negotiations is the question of who would provide security at the airport, with the Taliban insisting that foreign troops or security forces should not return.

Over the last decade, Doha has hosted the Taliban’s political leadership as well as peace negotiations among the United States, the Afghan government and the Taliban. Qatar also served as an intermediary between the United States and the Taliban seven years ago during negotiations over the release of Army Sgt. Bowe Bergdahl, who was exchanged for five Afghan Taliban prisoners then sent to Qatar.

As soon as the Taliban took over Kabul two weeks ago, Qatar’s influence was evident. The events were carried live on the Qatari-owned Al Jazeera news channel, and Al Jazeera’s reporters were given exclusive access to Taliban officials and to scenes of the group’s fighters entering Kabul’s presidential palace.

But much of Qatar’s role was hidden from public view. As Kabul was overrun, thousands of Afghans beseeched the Qataris not only to fly them out of the country but to help them get safely through the Taliban roadblocks and past desperate crowds surrounding the airport.

The Qataris emailed people wishing to leave, telling them to rendezvous at the Qatari ambassador’s hotel, where they were given snacks and drinks before piling into embassy vehicles. The ambassador, Saeed bin Mubarak Al Khayarin, personally led the rescue missions through the city with a handful of embassy staffers in a string of vehicles. His presence allowed them to navigate Taliban and American checkpoints.

“Our role as a mediator allowed us to facilitate things at the different checkpoints,” said an individual familiar with the operation, who spoke on the condition of anonymity because she wasn’t authorized to speak to the media.

The Qatari sorties scooped up a girls’ robotics team. They loaded an entire girls’ boarding school, more than 250 students and staff members, onto a convoy of buses that then crept through traffic behind the ambassador’s car. Qatari vehicles shuttled women’s rights activists who faced Taliban threats, a person familiar with the effort said.

Qatar’s actions have afforded Doha the kind of international recognition and prestige it has long sought.

But Qatar’s prominent role as a mediator in Afghanistan carries significant risks as well, given concerns about how the Taliban will govern, analysts said. Qatar has to be especially careful when it comes to the question of whether to formally recognize the Taliban government, said Bianco.

“They know the Taliban and they know what is going to happen in Afghanistan and it’s going to be horrible. They don’t want their image associated with the Taliban,” she said.

For that reason, she added, the Qataris were being careful to emphasize that whatever interactions they were having with the Taliban, “somebody else asked them to do it.”

The Taliban takeover of Afghanistan quickly overwhelmed the U.S. military’s ability to process evacuees and fly them to the United States or another country. The United States turned to Qatar, which agreed to allow a few thousand evacuees to land there and housed them on a temporary basis. Most were kept in the American sector of Al Udeid, a sprawling multinational air base outside Doha.

But Doha became a dangerous humanitarian bottleneck in one of the largest human airlifts in history. Flights to the base were suspended for several hours on Aug. 20 as the hot hangar where many evacuees were held — many for days — neared riot conditions.

As the Aug. 31 deadline for the American pullout arrived, more than 55,000 asylum seekers had landed in Doha, according to the U.S. military. The Qatari Foreign Ministry said about 20,000 were still housed in the country. They overflowed the American facilities, and many are now are held on the Qatari side of the air base and others in vacant, walled apartment complexes in industrial parts of the city.

“The Qataris have been incredible,” said an American who has worked closely with the evacuation operation. “This was a pretty ugly situation, but it would have been much worse if they weren’t doing this.”

Before the attacks of Sept. 11, 2001, it was Qatar’s neighbors, Saudi Arabia and the United Arab Emirates, that were seen as having influence with the Taliban. They were the only Arab countries to recognize the Taliban government in Kabul, reflecting ultraconservative support in both countries for Afghanistan’s hard-line rulers.

In the decades since, Saudi and Emirati priorities have changed. For the UAE in particular, countering the influence of all Islamist political movements has become a guiding foreign policy principle. Saudi Arabia and the UAE in recent years supported the former Afghan government, and the UAE is now hosting former Afghan president Ashraf Ghani, whose escape from Kabul sealed his government’s collapse.

Neither country is expected to rush to embrace Kabul’s new rulers or to establish diplomatic relations with the Taliban, at least any time soon, analysts and diplomats say. But both are likely to try some form of outreach to ensure that their interests are not compromised by the ascent of a potentially hostile government in Kabul.

Saudi Arabia’s foremost concern is to guarantee that the ­Taliban’s relationship with Iran does not blossom into a firm ­alliance that could work to the Saudis’ disadvantage, said Mohammed al-Sulami, president of the Rasanah International Institute of Iranian Studies, writing in the Saudi ­English-language newspaper Arab News.

The UAE’s biggest worry is the prospect that the Taliban’s ascent will breathe new life into the forces of Islamist extremism in the Middle East, said Abdulkhaleq Abdulla, a prominent political commentator based in Dubai. To protect its interests, the UAE is eager to find ways to influence the Taliban and is not averse to exploring contacts with the group, he said.

As Qatar exercises its influence, the country will want to avoid antagonizing Saudi Arabia and the UAE, rivals that have been resentful of Doha’s muscular foreign policy. Qatar and its neighbors recently patched up relations after a years-long rift.

The recent boost in Qatar’s prestige restores the tiny Gulf Arab country to the role it had fashioned for itself more than a decade ago as it sought to become an outsize power able to mediate among a range of countries and forces. But it could quickly face difficulties in navigating between the Taliban and Qatar’s other allies.

The airport dispute in Kabul represents the first challenge. The airport is now bereft of flights and littered with crippled planes and helicopters, the detritus of the long U.S. occupation. With thousands of Afghans still clamoring to leave, restoring service at the airport is a priority for the Biden administration and its allies.

As it has done for years, Qatar is trying to prod the Taliban to see the wisdom of compromise, Qatar’s foreign minister suggested in an interview published Monday in the Financial Times. “They don’t want to see a foreign security presence in their airport or their territory,” he said. “What we are trying to explain to them is that airport safety and security requires a lot more than securing the perimeters of the airport.”

As it presses the Taliban to compromise, Qatar has been urging the international community to meet the militants halfway.

“If they are going to have an inclusive government, if they are willing to engage in talks with the other [Afghan parties], and they are willing to take some steps that don’t bring Afghanistan back to the old era, these are going to be some reasonable things for the international community to continue cooperating with them on the way forward,” the foreign minister said.

Sly reported from London and Fahim from Istanbul.