Lord Geidt did not give a reason for leaving the role he took in April, but said it was the "right thing" to do.
His predecessor, Sir Alex Allan, quit in 2020 after Mr Johnson overruled him over a report into alleged bullying by Home Secretary Priti Patel.
The government said it was "surprised" by Lord Geidt's decision.
"Whilst we are disappointed, we thank Lord Geidt for his public service," a spokesperson added.
The spokesperson gave no reason for the resignation but said Lord Geidt had been asked this week "to provide advice on a commercially sensitive matter in the national interest, which has previously had cross-party support. No decision had been taken pending that advice".
No 10 did not make it clear what these comments referred to.
A Downing Street source told the BBC that Lord Geidt's resignation had been a "total surprise and a mystery" to the prime minister, adding: "Only on Monday Lord Geidt asked if he could stay on for six months."
Labour deputy leader Angela Rayner said: "The prime minister has now driven both of his own handpicked ethics advisers to resign in despair.
"If even they can't defend his conduct in office, how can anyone believe he is fit to govern?"
Liberal Democrat chief whip Wendy Chamberlain said: "When both of Boris Johnson's own ethics advisers have quit, it is obvious that he is the one who needs to go."
Former cabinet secretary Lord Turnbull said of Mr Johnson: "The pattern of behaviour is that anyone who has the power to criticise, obstruct or force him to change, he will try to reduce their power, suborn them or in the last resort wave them aside."
But he told BBC Newsnight that "the charge sheet of Boris Johnson's conduct is now so long that one accusation isn't going to make any difference", adding the prime minister would only leave office if he was removed as Tory leader by MPs.
It was reported that Lord Geidt had threatened to quit last month after the publication of the Sue Gray report into lockdown breaches in Downing Street unless Mr Johnson issued a public explanation for his conduct.
Appearing before a committee of MPs on Tuesday, Lord Geidt said: "Resignation is one of the rather blunt but few tools available to the adviser. I am glad that my frustrations were addressed in the way that they were."
But, in a brief written statement on Wednesday, he said: "With regret, I feel that it is right that I am resigning from my post as independent adviser on ministers' interests."
On Wednesday evening, Lord Geidt phoned the prime minister's principal private secretary to tell him he was resigning. Mr Johnson was informed of the decision at about 18:30 BST, shortly after finishing a phone call with Ukraine's President Volodymyr Zelensky.
But while the particular timing has surprised some in Downing Street, Lord Geidt's discomfort in the job has been evident for a while.
Just last month he had said in a report that it was a legitimate question to ask if Boris Johnson had breached the ministerial code by breaking Covid laws.
Lord Geidt also spelled out in the report that he didn't like the terms of his job - "the prevailing arrangements still remained insufficiently independent to be able to command the confidence of the public" as he put it.
But the truth is we don't yet know definitively why Lord Geidt resigned, as his resignation letter has not been published - which itself is unconventional.
The prime minister is expected to write back to Lord Geidt on Thursday morning, and that reply may well be made public.
Mr Johnson was fined in April over a surprise birthday party in his honour that he attended in Downing Street in June 2020.
Writing to Lord Geidt afterwards, he said there had been "no intent" to break Covid regulations", and that he had been "fully accountable to Parliament and the British people".
The ministerial code, which outlines the rules government ministers must follow, says there is an "overarching duty" on them to comply with the law.
If the code is broken, the convention in Westminster is for a minister to resign.
In his annual report on ministers' interests, published on 1 June, Lord Geidt said questions around Mr Johnson's behaviour had led to an "impression... the prime minister may be unwilling to have his own conduct judged against" the ministerial code.
He said that, when it came to the Partygate fine, "a legitimate question has arisen as to whether those facts alone might have constituted a breach of the overarching duty within the ministerial code of complying with the law".
Following Lord Geidt's resignation, Tory MP William Wragg, whose Public Administration and Constitutional Affairs Committee questioned the adviser on Tuesday, described him as "a person of great integrity, motivated by the highest ideals of public service".
Mr Wragg, a critic of Mr Johnson, added: "For the PM to lose one adviser on ministers' interests may be regarded as a misfortune. To lose two looks like carelessness."
Lord Geidt previously served as the Queen's private secretary and before that he was an Army intelligence officer and diplomat.