Surrounded by a bevy of bodyguards, U.S. Ambassador Zalmay Khalilzad quickly strode out of the house of Afghan presidential candidate Yunus Qanooni and through a gaggle of journalists waiting outside. "The real show is inside," said the impeccably dressed, Afghan-born envoy as he rushed to his armored car last week, after the country's first post-Taliban presidential election. He was being modest. These days, Khalilzad's highly persuasive diplomacy is the real show in Afghanistan.

Dressed in a silver gown and striped cape, Qanooni told the press that he was dropping his objection to the results of the country's Oct. 9 election in favor of an investigation by a panel of foreign experts into alleged electoral fraud--a Khalilzad suggestion. By the weekend, preliminary results showed incumbent Hamid Karzai with a lead comfortable enough to suggest victory. If that proves true, he owes Khalilzad a big thank-you. Heated charges of poll-rigging from Qanooni, Uzbek warlord Gen. Abdul Rashid Dostum and the 13 other candidates running against Karzai had threatened to sabotage the election, in which at least 75 percent of Afghanistan's more than 10 million registered voters cast ballots. Khalilzad spent three days negotiating with the malcontents, and seems to have rescued the election. "When you have a wobbly political process and weak institutions, you need a troubleshooter like Khalilzad," says Vikram Parekh, a senior analyst for the International Crisis Group in Kabul. "He may have saved the day."

Khalilzad, whom many Afghans have dubbed the "Viceroy," is arguably the most powerful man in Afghanistan. He speaks both major languages (Dari and Pashto), is steeped in Afghan customs, knows the difficult dynamics of tribal politics--and has at his disposal billions of dollars in U.S. economic aid as well as 20,000 U.S. troops. He is best of friends with Karzai; the two talk constantly via a secure cell-phone link.

Khalilzad's Washington connections are equally impressive. He is a Bush administration insider, close to the president and the conservative heavyweights at the Defense Department, including Secretary Donald Rumsfeld and his deputy, Paul Wolfowitz. He was an early advocate of invading Iraq, an enterprise driven more by ideology than on-the-ground reality.

But in Afghanistan, Khalilzad favors old-fashioned horse trading over political theory. In his one year in Kabul, he's favored stability over badly needed but potentially destabilizing reforms. For example, the envoy was faulted for not initially supporting Britain's suggestion to Karzai this summer to drop the warlord Marshal Mohammed Fahim as his vice-presidential candidate, for fear of the repercussions. Karzai eventually did dump Fahim from the ticket; it proved a popular move.

It's no secret that Karzai relies on Khalilzad's advice. The envoy is known to counsel the president on everything from which ceremonies to attend to how to handle an emergency. "When the president makes a major decision, he likes to have the ambassador on board," says a palace source. But to many Afghans and foreign analysts, Khalilzad is too cozy with the president. During the election campaign, critics complained that his highly visible presence alongside Karzai at school and road openings made it clear that the president was Khalilzad's and Washington's favorite candidate. "There's a widespread perception among Afghans that he was politically biased," says a representative of an international aid agency in Kabul. Khalilzad disputes that. While acknowledging that Karzai had advantages as the incumbent, "the presidential choice was for the Afghans [to make]," he says. "I did say that the president has been a good leader, and we work very well with him. I didn't hide that."

Some people in Afghanistan call Khalilzad a dealmaker. The ambassador prefers the term "intermediary," and says he is used by various leaders to pass messages to one another. "For reasons of saving face, they find that easier than going directly to a person." Presidential candidate Mohammad Mohaqeq, the ethnic Hazara warlord, claims that in a private meeting Khalilzad offered him two cabinet posts and two deputy ministries if he would drop out of the race and throw his support behind Karzai. Mohaqeq, who remained in the race, says the ambassador was friendly and did not pressure him. Khalilzad says that the tradeoff was Mohaqeq's idea, and that he passed it along to Karzai, but the two men could not reach an arrangement.

If Karzai wins big, as expected, he may want to break up his present coalition government and appoint a new, more administratively competent and reform-minded cabinet. Certainly, Afghanistan will be expected to handle more of its own affairs soon. Khalilzad doesn't know when he might leave Kabul, but if Bush is re-elected, he could get a top post with the new administration in Washington. If John Kerry wins, he's likely out of a job. Either way, Karzai will have to make do without his most effective problem-solver.