A controversial "sweetheart" tax deal between HM Revenue & Customs and Goldman Sachs, worth up to £20m, was agreed in part to avoid embarrassment to George Osborne, according to the government's former head of tax.

Dave Hartnett wrote that he decided to settle the long-running dispute after Goldman Sachs threatened to pull out of a prized new tax framework a week after the chancellor had announced that the bank had signed up to it.

The disclosures have emerged in an email and a witness statement placed before the high court on Wednesday where the campaign group UK Uncut Legal Action is asking for a judgment to declare that the 2010 settlement between Goldman and the tax authority was unlawful.

The court heard that Hartnett, then the permanent secretary for tax, personally overruled legal advice, the HMRC's own guidelines and its internal review board, which stated that HMRC was in a position to force Goldman Sachs to pay back the money owed.

The revelations may embarrass the chancellor who has been careful to avoid being dragged into an ongoing public debate over the Revenue's right to negotiate multimillion pound deals to end disputes with large corporations while pursuing ordinary taxpayers for small sums.

Hartnett struck a deal in a "handshake" with Goldman Sachs on 19 November 2010 to end a dispute over national insurance contribution payments dating back to the 1990s, the court was told.

But 11 days later, the deal was rejected by the Revenue's high-risk corporate programme board because it had failed to collect any interest on the disputed sum. That day, Osborne announced that the top 15 banks including Goldman Sachs, had signed up to a new code of conduct .

Hartnett sent an email on 7 December 2010 expressing concern that Goldman Sachs "went off the deep end" and threatened to withdraw from the government's bank code of practice, which was published in December 2009, when it was informed of the board's decision to reject the deal and force the bank to pay the interest. There could be a potential political embarrassment if the board's decision stood: "The risks here are major embarrassment to the ChX [chancellor of the exchequer], HMRC, the LBS [the large business service of the HMRC], you and me, not least if GS withdraw from the code."

In a witness statement for the court, Hartnett wrote that the Goldman Sachs threat to withdraw from the code of practice "would have embarrassed the chancellor".

Hartnett retired as head of tax last summer following stinging criticisms from the public accounts committee over the Goldman Sachs deal.

Murray Worthy, director of UK Uncut Legal Action, said the case exposed a "controversial cover-up at the heart of government by HMRC and former tax chief Dave Hartnett to avoid political embarrassment for George Osborne".

Rosa Curling, a solicitor from the law firm Leigh Day, which is representing UK Uncut Legal Action, said: "Our hope is that this legal action will not only declare this decision to 'let off' Goldman Sachs for tax owing unlawful, but also deter any more deals being done behind closed doors."

Ingrid Simler QC, for UK Uncut Legal Action, said HMRC reached a settlement in a dispute over national insurance due on bonuses with Goldman Sachs in 2010 without requiring the payment of interest. The potential cost to the taxpayer of the HMRC/Goldman Sachs settlement is officially put at £8m but an HMRC solicitor-turned-whistleblower, Osita Mba, claimed the sum could be as high as £20m.

The bank was allowed to skip the interest bill after Hartnett was wrongly advised there was a "legal impediment" to collecting it, said . The error was quickly noticed but, despite legal advice that the agreement with the bank was not binding, HMRC unlawfully withdrew a county court claim for what was owed without seeking to renegotiate, she said.

This went against HMRC guidelines stating that taxpayers should be treated equally and no discounts or deals should be done. Simler said: "The issues in this case are of great importance both to taxpayers and HMRC as well."

James Eadie QC, appearing for HMRC, said UK Uncut was using the courts "to pursue politics by other means". He said the Goldman Sachs deal had been examined in detail by the National Audit Office and found to be neither irrational nor improper.

The scale of the government's "sweetheart" tax deals – individual secret agreements drawn up between tax officials and corporations to settle disputes – was revealed this week by the Guardian after four settlements were shown to be worth £4.5bn between them.

If UK Uncut's challenge is successful, HMRC will come under further pressure to say how much tax was owed by each of the four unnamed companies before the deals were struck.

It also emerged on Thursday that Google and its auditor Ernst & Young will be recalled to parliament to restate their evidence on the internet search company's tax position following an investigation into its advertising sales practices.

A spokesperson for the Treasury said the chancellor could not comment on what he knew about Hartnett's decision to waive Goldman's alleged debt because of the ongoing court case. "Taxpayer confidentiality means that decisions are always made by HMRC without any ministerial knowledge or involvement," the spokesperson added.

• This article was amended on 3 May 2013. The original said that "a spokesperson for the Treasury said the chancellor would not comment".