Tony Blair descended into such a deep depression after the Iraq war that he told Gordon Brown and John Prescott he would quit No 10 the following summer – only to renege on the pledge within months, a new book by the Observer's Andrew Rawnsley reveals.

The former prime minister's physical and mental decline was so profound that he confided to friends that he "spaced out" several times during Prime Minister's Questions and often woke up in the middle of the night with sweat trickling down the back of his neck.

Rawnsley's explosive account is in The End of the Party, which is published on Monday , extracts from which appear in tomorrow's Observer. It lays bare, for the first time, how Blair was haunted and tormented by the deepening chaos and bloodshed in Iraq at the same time as being worn down by the constant psychological warfare being waged by Brown, his next-door neighbour in Downing Street, who was increasingly desperate to take his job.

While Blair's gift for presentation helped him hide his depression from the public and most of his staff, his private turmoil was so severe that he decided there was nothing for it but to hand over to Brown midway through his second term.

Rawnsley is the first journalist to detail how Blair, in those darkest days, made clear at a dinner with both Brown and Prescott in November 2003, and later in a telephone call to Prescott in spring 2004, that he would step down.

Sally Morgan, Blair's director of government relations, told Rawnsley: "Iraq was a quicksand swallowing him up. The atrocities. Those terrible photos [of Abu Ghraib]. And he started losing people who had supported him throughout. He was stuck in this long dark tunnel and could see no way out of it."

The book relates how Blair's special envoy in Iraq, the former UN ambassador, Sir Jeremy Greenstock, came to No 10 at the end of his service in Baghdad to brief the prime minister. Greenstock knew that his "very gloomy assessment" had made him highly unpopular in the building. Some at No 10 tried to keep him away, fearing the impact on Blair's collapsing morale. In Blair's den Greenstock warned him that the situation looked "unbelievably bad" and would get more desperate in the months to come. "What can we do?" pleaded Blair. "We have told them [the Americans] again and again what we think is necessary. If it doesn't happen, what can we do?" Greenstock was left with the image of the prime minister "tearing his hair" over Iraq and "throwing his hands in the air".

Rawnsley then charts how Blair – urged by his wife, Cherie, and closest political friends to pull back from the brink and deny Brown his chance – gradually recovered his self-belief and decided to fight on. The volte-face caused Brown's frustration to turn to rage.

On one occasion Brown went round to No 10 to get an answer. One of Blair's inner circle who witnessed this says: "Gordon was just losing it. He was behaving like a belligerent teenager. Just standing in the office shouting: 'When are you going to fucking go?' "

Blair's dark period throughout late 2003 and early 2004 was compounded by his heart complaint and anxiety that his young children were suffering at school because of the unpopular war their father had championed in Iraq. His friend and cabinet colleague Tessa Jowell says: "He was very low, he was very lonely and he was very tired."

In November 2003, Prescott, who was acting as "referee" between the prime minister and chancellor, hosted a dinner to discuss their differences and address the succession question. Next morning a visibly excited Brown told his key aides, Ed Miliband, Ed Balls, Spencer Livermore and Sue Nye, that Blair had assured him he was going in the summer. The four were sceptical, having heard about similar promises from Blair before. But by the spring Blair was to telephone Prescott and tell him he had settled on June as his departure date.

Behind the scenes, Blair's allies feared he was wobbling and were hatching a strategy to boost his morale. Jowell went to his study and told him: "You're going to get through this." Blair replied without conviction: "I'm fine, darling. Don't worry about me. I'm fine." Blair's friends noticed that women were better at bolstering him than men.

Peter Mandelson told him: "Come on. Buck up. Buck up. Think of what you've got to achieve. You're the best politician in this country by a mile."

After the June local elections of 2004, which were bad for Labour but not disastrous, Blair's zest for the job returned and he decided to stay to fight the 2005 election, which Labour won, though with a substantially reduced majority.

In autumn 2004, Blair declared he would fight the election but not lead Labour into a fourth one. But the statement led to relentless speculation about his departure date. Rawnsley reveals how, in the summer of 2006, Prescott was so frustrated with Blair's refusal to name a handover date that he threatened to resign as deputy prime minister unless he did so.