Senior figures in Whitehall and Downing Street became so fearful that the Scottish independence referendum could lead to the breakup of the United Kingdom that the Queen was asked to make a rare public intervention in the final days of the campaign.

Britain’s most senior civil servant and the Queen’s private secretary crafted a carefully worded intervention by the monarch, as No 10 experienced what one senior official described as “meltdown” in the closing stages of the campaign after polls showed growing support for a yes vote.

The discussions between Sir Jeremy Heywood, the cabinet secretary, and Sir Christopher Geidt for the palace, led the Queen to issue an appeal to the people of Scotland four days before the referendum in September to “think very carefully” before casting their vote.

The delicate negotiations in the runup to the intervention by the Queen, which were described by one senior Whitehall source as a warning to voters that they were facing “a decision filled with foreboding”, are revealed by the Guardian on the final day of a two-part series about the Scottish referendum campaign.

The Queen, who has been scrupulous during her 62-year reign in observing the impartiality expected of a constitutional monarch, intervened publicly on 14 September. Speaking after Sunday service outside Crathie Kirk near her Balmoral estate in Aberdeenshire, the Queen told a wellwisher: “Well, I hope people will think very carefully about the future.”

The Queen speaks to wellwishers during a walkabout after the morning service at Crathie Church before the Referendum. Photograph: Donald Stewart

She spoke out after senior Whitehall figures, who were apprised of David Cameron’s concerns that the yes camp was developing an ominous momentum in the final period of the campaign, suggested to the palace that an intervention by the Queen would be helpful.

The suggestion was made during the last few weeks of the referendum after a YouGov/Times poll on Tuesday 2 September reported a six-point fall in support for the pro-UK side in a month. Key figures in Downing Street and Whitehall, led by the prime minister and the cabinet secretary, assessed all the options they could deploy to halt what appeared to be the yes side’s momentum.

Cameron discussed the referendum with the Queen a week before her public intervention when he travelled to Balmoral for his annual visit. On that trip, there was a particular focus on the referendum when the campaign was electrified by the publication of another poll, a Sunday Times/YouGov survey on 7 September, the final day of the prime minister’s Balmoral visit, which gave the yes side its first lead – by 51% to 49%.

The Whitehall source added that the referendum was discussed during Cameron’s Balmoral stay. “I don’t think it was frosty. I think there might have been the odd humorous comment over the porridge about supposing he had some work to do next week.”

The prime minister is said to have talked about the Queen’s humour on the occasion to friends. There was also a suggestion that the atmosphere had, at times, been frosty. You could imagine the chilly atmosphere at the breakfast table, the prime minister is said to have remarked to friends afterwards.

Discussions about interventions by the monarch are by convention a matter for the cabinet secretary and palace officials. This explains why the contacts in the runup to the Queen’s public comments took place between Heywood and Geidt, described by the Whitehall source as the two key figures at the heart of Britain’s “deep state”.

The two men are understood to have initially discussed the wisdom of a public intervention by the monarch, who is scrupulously impartial. Once it became clear that the Queen was minded to speak out, Geidt and Heywood then needed to fashion language which, while broadly neutral, would leave nobody in any doubt about her support for the union.

There was a determination to ensure she did not cross a line, as some said she did when she spoke of the benefits of the UK in her silver jubilee address to a joint session of parliament in 1977. In remarks which were seen as an attempt by the Labour government of Jim Callaghan to warn of the dangers posed by the Scottish National party after it had won 11 seats in the October 1974 general election, she said: “I cannot forget that I was crowned Queen of the United Kingdom of Great Britain and Northern Ireland. Perhaps this jubilee is a time to remind ourselves of the benefits which union has conferred, at home and in our international dealings, on the inhabitants of all parts of this United Kingdom.”

The Whitehall source said the Queen’s intervention was carefully calibrated. “She knew exactly what she was doing, which is, there are two possible responses on the referendum. [They are] either: one, you buy into this is a fantastic festival of democracy, or two, you suggest this is a decision filled with foreboding. So by saying I hope people will think carefully you imply the second. So if they’d said: ‘What do you think of the referendum ma’am?’ and she’d said: ‘Oh it’s lovely’, that would be very different. Without her taking a side, it cast just the right element of doubt over the nature of the decision.”

Gordon Brown: ‘pivotal role’ in referendum campaign. Photograph: Dylan Martinez/Reuters

The final day of the Guardian’s Scotland referendum series also highlights Gordon Brown’s pivotal role in helping to save the UK in the final period of campaigning. Cameron and George Osborne were so nervous about a yes vote, which would have thrown his premiership into a potentially fatal crisis, that camp beds were laid on for senior officials in Downing Street on the night of the referendum count.

The dominance of the referendum explains why a relieved Cameron told the former New York mayor Michael Bloomberg, in an overheard conversation days after the referendum, that the Queen had “purred down the line” when he told her the result.

David Cameron tells Michael Bloomberg ‘the Queen purred’ Guardian

Buckingham Palace declined to comment on the discussions between Geidt and Heywood. A Cabinet Office spokeswoman said: “We do not comment on discussions between the Queen’s private secretary and civil servants.” A Downing Street spokesman said: “No comment.”

A palace spokesman said of the prime minister’s discussions with the Queen at Balmoral: “As is the convention, we do not comment on conversations between the prime minister and the Queen.” A Downing Street spokesman said: “We do not discuss the prime minister’s conversations with Her Majesty the Queen.”

• This article was amended on 17 December 2014. An earlier version said Samantha Cameron had accompanied the prime minister to Balmoral in September. That is not the case.