A £520,000 bequest shared between the Tories and Lib Dems will be handed to the Treasury, amid claims it was meant to be left to the nation.
Retired nurse Joan Edwards left the money to "whichever government is in office" and it was divided up as a donation between the coalition parties.
But after some criticism, both parties said they would give up the cash.
David Cameron said it was the "right decision" having seen the wording of Miss Edwards's will.
He said it was his understanding that the executors of the will had decided the money should go to the parties of government - but the words in the will suggested it was meant to benefit the nation.
The money would go to the Treasury to "pay down the national debt", he said which would "meet the spirit" of what Miss Edwards intended.
"We accepted the money in good faith... but having been able to look at the wording of the will and consider the matter, I think this is the right decision," the prime minister said.
Miss Edwards, who died last year aged 90, was a surprise name on a list of party donors released on Tuesday by the Electoral Commission.
The Bristol woman's £520,000 bequest made her the biggest donor to the Conservative and Liberal Democrat parties in the second quarter of the year, dwarfing the usual contributions by City financiers and other big money donors.
Lib Dem and Conservative sources said the donation had come "out of the blue" and that Ms Edwards' will had specified the money should go to "whoever was in government".
The parties said they had been guided by the will's executors in deciding to divide it up according to the number of MPs and ministers of each party - the Tories received £420,576 while the Lib Dems received £99,423.
But when the full wording of the will emerged, the coalition partners faced calls from all sides for them to hand the money to the Treasury, amid claims it had not been intended for party political purposes.
Miss Edwards' will specified the money should go to "whichever government is in office at the date of my death for the government in their absolute discretion to use as they may think fit".
Labour former deputy prime minister Lord Prescott said the parties "must hand back" the cash, while Labour backbencher Ian Austin urged them to "do the decent thing".
Shadow Defence Secretary Jim Murphy wrote on Twitter: "Unless tomorrow's explanation of this is much clearer & more credible, this looks dodgy as hell by Tories & Libs".
Conservative Zac Goldsmith also joined in the criticism, saying no one could believe "this lady wanted her money squandered on electioneering" while his fellow Tory MP Conor Burns wrote on Twitter that it had been "poor judgement" to accept the money.
However Davis Wood, the solicitors handling Miss Edwards's estate, said that when the will was drafted in 2001, they had checked with her "the unusual nature of her proposed bequest".
"It was confirmed by Miss Edwards at the time of her instructions that her estate was to be left to whichever political party formed the government at the date of her death," they said.
Joan Seville, 85, who had known Miss Edwards for more than 40 years, told the BBC she was a former midwife, a very private person and a church-goer.
But Mrs Seville said she did not think Miss Edwards was a particularly political person, although she did believe it was important that women voted.
About 16 people from her church and one neighbour attended her funeral last year. Ms Seville said Miss Edwards' only family had been her mother, who lived with her until she died some years ago.
She "wouldn't have dreamed" that Miss Edwards would have had such a large will.
It is understood that the solicitors handling Miss Edward's estate had contacted HM Treasury solicitors and the office of the Attorney General, Conservative MP Dominic Grieve, seeking advice, before finalising the bequest.
A spokesman for the attorney general's office said the Treasury Solicitor had replied on Mr Grieve's behalf suggesting "further steps the executors might wish to take to identify the correct recipient of the bequest" but did not advise on who should receive the money.
The Attorney General's office told the BBC that the will had not been dealt with by Mr Grieve himself.