The remains of US Army Maj. Gen. Harold Greene are transferred from an aircraft at Dover Air Force Base in Dover, Delaware. Secretary of State John Kerry (inset) made a surprise visit to Afghanistan following the general's shooting. Photo: EPA, Getty Images (inset)

KABUL, Afghanistan — U.S. Secretary of State John Kerry flew to Afghanistan late Thursday on an unannounced visit to press the country’s two feuding presidential candidates on the urgency of ending a bitter dispute over June elections and forming a new government by early September.

Kerry’s visit comes as the election results are being audited in a process that he brokered last month. Senior officials traveling with Kerry said the U.S. wants to see the ultimate winner inaugurated — and a new “chief executive officer” chosen by the loser under the terms of a national unity government compromise — before the upcoming NATO summit in Wales Sept. 4.

At that summit, NATO leaders are hoping to make decisions about their nations’ role in Afghanistan after the end of the year, when most combat troops will be withdrawn.

Kerry’s arrival in Kabul follows Tuesday’s killing of a U.S. general by an Afghan soldier at the national defense university, an incident that underscored the tensions that persist as the U.S. combat role winds down. The political uncertainty that Kerry is trying to address is another complicating factor in the transition.

Kerry planned to meet both candidates — former Foreign Minister Abdullah Abdullah and former Finance Minister Ashraf Ghani Ahmadzai — on Thursday night and then meet with current Afghan President Hamid Karzai before he leaves Friday for an Asian security conference in Myanmar.

The audit of ballots from the June 14 presidential runoff resumed Monday after a holiday break and is still likely to take weeks to complete.

Faced with the prospect of the two sides clashing amid bitter disputes over the vote tallies, Kerry made an emergency visit to Kabul and persuaded the two sides to agree to a full audit of the ballots.

Kerry has spoken by phone several times with Abdullah and Ghani Ahmadzai since, urging them to continue to support the electoral process and a framework for a national unity government agreed to during his last visit.

In addition to the appointment of a CEO by the losing candidate, the framework also calls for that post to be replaced by a new prime minister position after ratification by a Loya Jirga, or nationwide conference of elders and tribal chiefs.

Kerry has stressed the urgency of accelerating the audit and implementing the framework agreement following the Muslim holy month of Ramadan, which ended in late July, and has reminded the candidates that Afghans and the country’s international partners need clarity from the process and confidence that they and their supporters will be able to work together to implement reforms no matter who wins.

Both candidates have pledged to sign a bilateral security agreement with the U.S. that would give legal protections to residual American forces, but Washington would like the pact in place as soon as possible.

The political wrangling has cast a pall over U.S. and NATO planning for the withdrawal of combat troops. Tuesday’s killing of U.S. Maj. Gen. Harold Greene by an Afghan soldier, who also shot and wounded 15 other allied troops, has only exacerbated concerns.

On Wednesday, however, U.S. Defense Secretary Chuck Hagel said the incident would not affect “our decision or resolve to continue moving forward on an enduring presence post-2014.”

Speaking in Germany, Hagel also rejected suggestions the military should have greater flexibility to rethink its withdrawal and allow commanders to make some decisions based on the conditions in the country. He said President Barack Obama’s decision to cut the number of troops in Afghanistan to less than 5,000 by the end of 2015 and leave only about 1,000 in a security office after the end of 2016 will stand.

The top U.S. commander in Afghanistan told Congress last month that commanders didn’t recommend a hard date for the troop withdrawal at the end of 2016 and would rather re-evaluate the decisions based on conditions there. During his confirmation hearing to be Marine commandant, Gen. Joseph Dunford said the insurgent threat and the capabilities of the Afghan forces should be considered.

Dunford also told the panel that U.S. and Afghan military leaders would have preferred to see American officials be “a bit more ambiguous” about the troop numbers and not telegraph to the enemy that international forces would leave by the end of 2016.

But Hagel said Wednesday that while commanders always need some flexibility, “generally those decisions have been made. We’re proceeding on that basis.”