The timeless debate about whether history is shaped by accident or design found a definitive answer in Britain this year when a higgledy-piggledy series of events over three days at Westminster in August inadvertently fashioned events in Syria and beyond.

A decision to recall parliament to approve military action against the Assad regime after a chemical weapons attack, casually announced in a tweet by David Cameron, was widely seen as a formality that would ensure Britain would once again line up alongside the United States.

All these calculations had to be cast aside when MPs rejected rival – but almost identical – coalition and Labour amendments on 29 August. This prompted an enraged prime minister to issue a surprise statement from the dispatch box definitively ruling out British involvement in military strikes, creating a space which eventually led to the US-Russian deal on chemical weapons a few weeks later.

"There were a whole series of accidents that ended up in a better place," one minister says.

The Guardian has spoken to senior figures from the three main parties to reconstruct the fast-moving events over three days in August – from Tuesday 27, when the recall of parliament was announced, to the late evening of Thursday 29, when the prime minister was defeated – to assess the most important week in British foreign policy in 2013. The interviews show Barack Obama and Cameron were at times powerless to shape events. And there is a lingering resentment in Downing Street that Britain worked hard to persuade Obama to stand up to Assad but was then bounced by the White House into supporting military action without giving Cameron time to secure backing from MPs.

"What is Obama's foreign policy? Don't ask me, your guess is as good as mine," an exasperated figure close to the prime minister said.

"There were too many accidents," a senior Tory said. "A miscalculation by the Cameron government – and it could have gone either way – ended up handing Obama a get out of jail card."

At the start of the week Obama let the prime minister know that he might take military action on Friday 30 August, Saturday 31 or Sunday 1 September. Cameron, who was bound by an undertaking to recall parliament from its summer recess if events in Syria moved towards action, responded in a lackadaisical way, allowing Downing Street to announce in a tweet that parliament was to be recalled after the prime minister cut short his holiday in Cornwall.

Downing Street knew it would face a significant Tory rebellion but was confident it would win a vote to approve military action on two grounds: the vast majority of Conservative MPs would support the prime minister and the Labour party would fall into line. Both those calculations turned out to be wrong.

One senior figure tasked with assessing the Tory mood quickly realised the prime minister was in trouble when MPs started assembling at Westminster on the Wednesday. "I became aware of deep concern and reservations. There were people who were middle-ranking, junior ministers who were intimating that they were prepared to resign over this.

"It became obvious in the 24 hours before the vote that the figures just didn't add up. You can always tell when things are getting in difficulty – the whips are like starlings, they are squawking and rushing around. It just looked bad."

Cameron knew all week that he needed the support of Ed Miliband and held a number of meetings with the Labour leader in Downing Street. These ended up poisoning relations between the two leaders, with the prime minister telling Miliband at one point that he was "letting down America" and siding with Russia.

Cameron believes he went out on a limb for Miliband by agreeing on the eve of the parliamentary vote to consult MPs a second time at a later date to approve military action. Even then the Labour leader tabled his own amendment; deadlock ensued after both amendments went down. Miliband was astonished when his tactics ended up defeating the government and taking military action off the table when the prime minister stood up after the vote and said to him: "I get that [the defeat] and the government will act accordingly."

One Labour source said: "Ed thought it was a careful forward defensive shot but it went over the boundary for six."

Douglas Alexander, the shadow foreign secretary, who was Miliband's closest confidant during the week, believes Labour acted in an entirely principled way because the two amendments contained genuine differences. "The amendment that we tabled on the Thursday evening contained substantive tests and conditions that simply weren't present in the government motion," he told the Guardian after the vote.

Labour believes Cameron has only himself to blame. "Downing Street just did not get the number of people in the Tory party that believed they had not thought this through," a senior Labour whip said. "They also underestimated Ed's determination to do this properly and legally."

Miliband's supporters say the way he faced down Cameron and Nick Clegg, who also joined their meetings, show his toughness and his foresight in understanding the legacy of the Iraq war better than the prime minister and his deputy. They believe he stopped a rush to war and has been vindicated by subsequent events.

Behind the scenes, however, Miliband found that he had something in common with Cameron and Obama: he, too, was not master of events. A series of conference calls held by Miliband's office with members of the parliamentary Labour party early in the week produced a clear message: proceed with care because military action at that time had the feel of a repeat of Iraq.

Concerns were later voiced about Miliband's style of leadership after he seemed more interested in consulting his long-term ally Hilary Benn, the shadow environment secretary, than Jim Murphy, his shadow defence secretary, and Ivan Lewis, his shadow international development secretary. Murphy, who was deeply sceptical about Miliband's stance and who confronted the Labour leader in his office on the day of the vote, was a political casualty. Weeks later he was demoted to the international development portfolio in what was described as a "punishment beating" after Miliband took exception to a blog three days after the vote in which he expressed coded unease at the outcome.

The Tories, who initially turned against Miliband for destroying any chance of British involvement in military strikes, now believe he did the prime minister a favour on the grounds that he would have lost a later vote authorising military action.

But there are concerns that the miscalculations by Downing Street and the Tory whips, who told some MPs on the eve of the vote not to bother turning up because "Red Ed" was "in the bag", have highlighted fundamental flaws in the No 10 intelligence operation.

Downing Street moved to improve matters by clearing out the whips' office in the reshuffle in October. Sir George Young, the chief whip, remained. "Sir George is an upper-class Carson who was called in to give his view during the Syria week," one Tory says. Greg Hands, one of George Osborne's closest allies, was drafted as his Conservative deputy.

But some Tories say it is important to remember that Osborne, who ran what was described as a "shadow whips' office" on the day of the vote, failed to persuade colleagues.

"A massive amount of arm twisting was going on not so much by the whips but by the shadow whips office – the chancellor and his friends," said one. "Two of the most gungho people were Brothers Osborne and [Michael] Gove leading firmly from the front at that stage."

Malcolm Rifkind, a former Tory foreign secretary, said: "Our whips were too lax. Until it is signed, sealed and delivered you cannot assume that an opposition, even if they have agreed, may not change their mind."