When a smaller nation takes its turn on the UN security council it's a chance to strut its stuff on the world stage, to be where the action is. Ambassadors from developing countries will be courted and flattered by the big boys from Washington, Paris, Moscow, London and Beijing, their opinions sought, their every need the subject of sudden sympathetic interest. But in times of heightened global tension, this courtship can segue into bribery, and flattery can swiftly turn into something more loaded.

"What's the going rate for a vote on the security council these days?" Trevor McDonald goaded Tony Blair in an interview five years ago in the Map Room of the Foreign and Commonwealth Office before an audience of women opposed to war, prompting a furious call from Alastair Campbell to the producer threatening to pull his man out unless the producer got his man under control.

Blair, whose diplomatic priority was then to wrest from a reluctant security council a resolution authorising the use of force in Iraq, gave McDonald the weary, pained look of the unfairly accused. Had the prime minister only cast his mind back to 1990 he might have been able to come up with a figure. Then it was Yemen's turn on the security council and it was Yemen's bad luck that the US and Britain were at that very moment looking for UN backing for the first Gulf war. When it failed to support the necessary resolution punishment was swift: £12m in annual aid was withdrawn. US diplomats told Yemen's ambassador he'd just cast the most expensive vote of his career. We may be talking about an organisation with high and noble ideals, but UN diplomacy can be pretty rough stuff. It was to get a lot rougher in the run-up to the 2003 invasion of Iraq, and the consequences were to be far-reaching and dramatic.

Five years ago this month, with American and British troops massing on Iraq's borders, it was the turn of, among others, Mexico, Chile, Cameroon, Pakistan, Angola and Guinea to sit on the security council. They were coming under intense pressure from the US and Britain to vote for a second resolution. There were economic complications: the Chileans were waiting for a free-trade agreement with the US to be ratified; Angola was hoping for an aid package to be approved. As one ambassador said, "No one was explicitly saying the aid was linked to our support, but everything was happening at the same time."

Over the past months I have worked with a team of BBC researchers to find out what really happened in those difficult days. We scrutinised documents and interviewed those involved, and I wrote about what we found out in eight short films about the run-up to the invasion, to be shown next week. Our film set in the UN describes the efforts to persuade those states unwilling to vote for the second resolution. Jeremy Greenstock, Britain's ambassador to the UN, arranged for his Mexican counterpart, Adolfo Aguilar Zínser, to be briefed by MI6 on the existence of weapons of mass destruction. Aguilar, who died in a car crash in 2005, was an acutely intelligent, ambitious, articulate and sophisticated diplomat, and he was determined not to be steamrollered or bamboozled. The briefing, he recalled with some bemusement, took place in a sealed room, amid conditions of elaborate security, all very James Bond. Here British intelligence officers spread out maps of alleged WMD sites on the table.

The brains of politicians and diplomats - journalists too - have a tendency to turn to mush during briefings with secret agents. Not Aguilar. The former lawyer's questioning was forensic and insistent: he asked the MI6 officers directly if they had "full proof" of evidence of WMD on these sites. According to his account, the officers said, "No, we don't. We can't tell you this is a weapon here. But we have reasons to believe there are weapons because we can document the way Saddam hides them."

Aguilar then asked where the Iraqi leader was hiding the weapons. The agents had suspicions but weren't able to say for certain. As Aguilar noted, the rather more qualified private answers of the MI6 officers contrasted with Blair's categorical public statements that there were WMD in Iraq. The Mexican diplomat asked an obvious but pertinent question: what was the correlation between how well a weapon was hidden and the ease with which it could be used? The answer was that it was "a negative correlation"; that is, the better a weapon was hidden, the more time it would take to prepare for use. It was the British case that Hans Blix and the UN weapons inspectors could not find WMD because they were so well concealed. But it was also the British case that the weapons could be prepared for use in 45 minutes. The two positions, Aguilar was pointing out, could not be reconciled.

When Aguilar went into the sealed room, he did not know if Saddam had weapons of mass destruction. When he came out, he was even less sure. He was not alone. He cooperated closely with his friend from Chile, Ambassador Juan Gabriel Valdés, in trying to forge a counterweight to Britain and the US over the second resolution. They took a legalistic position: in order for conditions to be met under UN charter, the threat must be evident before force can be authorised. They argued there was a mechanism in place - the weapons inspectors - to determine the extent and reality of the threat. Along with Cameroon, Angola, Pakistan and Guinea, Mexico and Chile became known as the "six undecideds". Aguilar maintained this was a misnomer: they were not undecided but very much opposed to war. They argued that Blix and co be given the time they needed to finish their job. Blix would either find the weapons and destroy them or confirm WMD did not exist, in which case there would be no evident threat and no legal basis for war. It was not a position that suited the British and Americans, whose diplomacy was being driven by the military timetable, for as we now know Blair had signed up to war regardless of whether he got a second resolution.

Aware that they were facing an organised attempt to counter their influence on the security council, the Americans spoke to Aguilar's foreign minister. In the view of the US, Aguilar was "unhelpful"; "an unguided missile". Early in March 2003, Aguilar was recalled to Mexico for urgent talks with his president, Vicente Fox. Somehow the ambassador managed to persuade Fox to keep him in place, at least for now, and Aguilar returned to New York as the British made their final diplomatic push to get the resolution. By then Aguilar and his colleagues from the undecideds were the target of an extensive bugging operation, which was exposed by Observer journalists who got hold of a memo advising senior NSA officials that the agency was "mounting a surge" aimed at gleaning information not only on how delegations on the security council would vote on any second resolution on Iraq, but also "policies", "negotiating positions", "alliances" and "dependencies" - the "whole gamut of information that could give US policymakers an edge in obtaining results favourable to US goals or to head off surprises". It was an aggressive surveillance operation, involving interception of the home and office telephones and the emails of UN delegates in New York. After one private meeting, almost certainly bugged, at which a tentative strategy based on giving Blix more time was mooted, ambassadors were told by the US that this was to go no further.

Greenstock was soon to have even graver cause for anxiety. As the push for the second resolution reached its climax, he began to suspect there might actually be an attempt to table a counter resolution. The impact would be devastating. Blair had based his entire PR campaign on the importance of upholding the will of the international community. It was bad enough that he couldn't persuade that community to support war; it would be calamitous if he were to face a counter resolution, even if it were eventually voted down or vetoed, forbidding Britain and America from using force. A war, which was legally dubious, would then be explicitly illegitimate.

During his grilling in the Foreign Office's Map Room on March 10, the prime minister insisted time and again he was "working flat out" for a second resolution, and officials let it be known they were still confident of success. He had every reason to work flat out. Around 2 million people had marched through London the previous month to protest against invasion. As Blair conceded, many people were uneasy about the legality of the war precisely because it did not yet have UN backing.

Within the Labour party there was also growing unease. Backbenchers on the left like John McDonnell and Jeremy Corbyn would never be brought round to the case for war. But signs of disaffection in normally loyal quarters were spreading. Robin Cook, then leader of the House, was the most high profile opponent of the war (he was to resign on March 17), but there were others.

Anne Campbell, MP for Cambridge. was then parliamentary private secretary to her longtime friend Patricia Hewitt, secretary of state for trade and industry. A Blairite loyalist, Campbell had never once voted against the government. It was a harrowing time. She believed in Blair, she believed in the project, she believed in the government - but she did not believe in the war, at least not without a second resolution. As the day of the Commons vote on war neared, March 18, she found herself in the desperate position of having to contemplate resignation.

Another loyalist tormented by the prospect of war was Paul Stinchcombe, MP for Wellingborough. Stinchcombe had wanted to be an Labour MP all his life and had joined the party out of a belief in social justice and respect for human rights. A committed Christian, he was now being asked to vote for a war in which there would be civilian casualties. It revolted him. Government whips arranged meetings with Blair and also with Cherie, in whose chambers Stinchcombe, a human rights barrister, had worked. When we went to see him, Stinchcombe recalled long ethical and legal discussions with colleagues in parliament, and also the more emotionally fraught confrontations he had with his teenage daughter Yasmin, a convinced opponent of the war. He eventually voted for, but the decision traumatised him. In the 2005 general election both he and Campbell lost their seats.

A stone's throw from the palace of Westminster, Elizabeth Wilmshurst was deputy legal adviser in the FCO, with almost 30 years' service. By March 10, she was digesting the secret legal advice to the government from Lord Goldsmith. The attorney general's memo concluded that war would be legal without a second resolution, although he included the important caveat that he could not guarantee "that if the matter ever came before a court I would be confident that the court would agree with this view" (on March 17 Goldsmith finessed his advice, removing this and other qualifications).

Wilmshurst did not agree with his conclusion. In international law there are three justifications for the use of force. First, self-defence. In his advice, Goldsmith accepted the self-defence argument was not applicable. Second, to avert an overwhelming humanitarian catastrophe. Again, he said that would not be an appropriate basis for action in Iraq. Third, you can use force if the security council says you can use force. And, of course, as of March 10, the security council had still not said that the British and Americans could use force. In New York four days later, on March 14, Greenstock threw in the towel and declared there would be no vote on the second resolution. Wilmshurst had to make a decision. She is not the kind of person to make a fuss and she certainly did not want media attention. After she made known her difficulty to her superiors she agreed to retire quietly. The public did not find out about her decision to go until some weeks later.

In spite of all the pressure, the bugging, the inducements and the warnings, Aguilar and the undecideds stood firm. There was no second resolution. Britain and America invaded Iraq without UN support. Aguilar did not last long in New York. In November, the US called again on Fox to remove him. This time the Mexican president capitulated.

In the British version, there was every chance of getting a second resolution had it not been for the perfidious French. But as Aguilar said, it was convenient for the British to pretend the second resolution failed because of President Jacques Chirac and the threatened French veto. It would have been far less palatable to acknowledge the truth: that Britain had four votes on the security council out of 15 (the UK, US, Spain and Bulgaria). There never was going to be a second resolution.

· 10 Days to War, starring Kenneth Branagh, Tom Conti, Patrick Malahide, Juliet Stevenson and Harriet Walter, starts on BBC2 on March 10 at 10.30pm