Top US diplomats connived in delaying an Afghan presidential election in 2009 and then tried to manipulate the outcome in a "clumsy and failed putsch" that aimed to oust Hamid Karzai, the US defence secretary at the time, Robert Gates, has said.

In a memoir to be published next week, less than three months before Afghanistan's next critical presidential poll, Gates says the 2009 election was "ugly" and that, though the president was "tainted", "our hands were dirty as well".

The revelation was greeted with triumph in the presidential palace in Kabul, where Karzai and his confidantes have long accused the US of trying to push him out of office. Washington has always insisted it was an impartial supporter of the democratic process.

The row over the election has poisoned ties between the two capitals ever since, and contributed to Karzai's decision late last year to delay signing a long-term security deal that would keep US troops on Afghan soil past 2014.

"There was no exaggeration on our side when talking about this, and now a very senior US official is accepting this fact and talking about it," Karzai's spokesman, Aimal Faizi, told the Guardian. Another presidential election is set for April, and Karzai has said he will not finalise the bilateral security agreement without guarantees that the US won't try to meddle.

"As they say, 'once bitten, twice shy'. That's why, because of this bad experience of the past, we are trying to make sure we do not allow such interference from outside," Faizi added.

The account of behind-doors efforts to manipulate the election appears in Gates's upcoming book, Duty: Memoirs of a Secretary at War. It covers Gates's service under both Presidents George W Bush and his successor Barack Obama, who surprised Washington with his decision to keep on a man selected by a Republican leader.

The Afghan election, eventually held in August 2009, was marred by widespread fraud and badly damaged Karzai's international reputation, but Gates laments that the US too was stained by underhand efforts to control the result.

"It was all ugly: our partner, the president of Afghanistan, was tainted, and our hands were dirty as well," Gates wrote. "Our future dealings with Karzai, always hugely problematic, and his criticisms of us, are at least more understandable in the context of our clumsy and failed putsch."

Senior diplomat Richard Holbrooke, then special envoy for Afghanistan and Pakistan, paid public lip-service to the idea of a level playing field, but was working behind the scenes to ensure the opposite, Gates said. He first supported an unconstitutional three-month delay to the election and then, with the help of the US ambassador in Kabul, Karl Eikenberry, backed Karzai's rivals, he alleged. Holbrooke aimed to push the poll to a second round that the incumbent would lose, but the candidate who got through cancelled the vote and conceded to Karzai.

"Holbrooke was doing his best to bring about the defeat of Karzai," Gates writes. "What he really wanted was to have enough credible candidates running to deny Karzai a majority in the election, thus forcing a runoff in which he could be defeated."

Tactics included advising candidates, attending rallies and organising high-profile photo opportunities, according to Gates, who is highly critical of Karzai elsewhere in the book.

He ponders if the president "listened to anyone but the conspiracy-minded", but points out that the Afghan leader could not have missed what Holbrooke was doing. "Karzai might not be a great president, but he sure as hell knew what was going on in his own capital and was well aware of the American efforts to unseat him," adds Gates.

A spokeswoman for the US National Security Council said Gates's account was untrue.

"I worked for Ambassador Eikenberry at the time, watched all of this up close and personal, and that's just categorically false," Caitlin Hayden said in an email response to questions. "The US's interest was in a stable Afghanistan, with credible democratic elections – not in helping any candidate win or lose."

But Karzai's spokesman Faizi said US meddling in the poll went beyond the former defence secretary's account. Americans presented Karzai with a choice of accepting a chief executive to work beside him or facing the runoff he was likely to lose, he said. Karzai chose the runoff.

Anger over the election has been just one source of tension between the two uneasy allies as they try to negotiate an extension to their political marriage of convenience. Although combat troops are due home by the end of this year, both sides say they want some US soldiers to stay on.

But disagreements over issues from civilian casualties and US operations in Afghan homes, to the state of peace talks with the Taliban, have held up the pact.The current US ambassador to Kabul warned in a secret cable leaked to the Washington Post this week that the deal is unlikely to be signed early this year as US officials want. They say they need time to plan a withdrawal and the deployment of any additional troops.

Washington insists that if there is no deal, it will resort to its "zero option" and take all troops home, but Karzai has said he considers that an empty threat.

In office Robert Gates was considered a model of discreet and bipartisan leadership. Appointed by George W Bush, he was kept on in the job by Barack Obama.

Aside from revealing US efforts to skew the outcome of the Afghan elections, other highlights include:

• An attack on vice-president Joe Biden, who he says lacks basic judgement: "I think he has been wrong on nearly every major foreign policy and national security issue over the past four decades"; • The revelation that he thinks Obama "can't stand" Karzai;

• His questioning of the decision to send extra troops to die in an Afghan war that he himself never appeared to believe in: "I never doubted Obama's support for the troops, only his support for their mission.