In Iran last week the British foreign secretary, Jeremy Hunt, gave his opposite number, Javad Zarif, a copy of Nelson Mandela’s The Long March to Freedom to give to Nazanin Zaghari-Ratcliffe, the Iranian-British dual national who is in jail in Tehran. It was a tactic he picked up from Margaret Thatcher, who gave books on liberty to Russian leaders such as Andrei Gromyko to hand to Andrei Sakharov, the Russian dissident for whose freedom she had been campaigning.

By the middle of last week, Hunt was starting to think he might have to find a similar volume to give to the rulers of the United Arab Emirates to pass on to Matthew Hedges, after the UAE unexpectedly imposed a life sentence on the British academic. The Foreign Office was genuinely shocked by the severity of the sentence, and thought it ran counter to the personal assurances Hunt thought he had received from the UAE leadership.

The fear in the FCO was that Hedges’ fate was somehow linked to the growing differences between Britain and the UAE-Saudi coalition over the war in Yemen. The Saudis were being pressed to agree to a UN resolution supporting a cessation of hostilities, but there was resistance both in Riyadh and Abu Dhabi.

Play Video 1:11 'Mr Hedges will be permitted to leave' : UAE pardons UK academic – video

The sentence might even have been a sign of disapproval at the way Hunt has refused to rule out the possibility that the Saudi crown prince, Mohammed bin Salman, ordered the murder of Jamal Khashoggi in the Saudi consulate in Istanbul. Was Hedges, arrested in May, suddenly becoming a diplomatic tool, just as Zaghari-Ratcliffe appeared to have become in Iran ?

The UAE’s ambassador to London, Sulaiman Almazroui, insisted all last week that this was not the case, briefing that such fears were misplaced. Nevertheless, something had gone badly wrong and been allowed to fester. An unwelcome light was being thrown on the UAE’s judicial system, its approach to academic freedom and its image as a soft power in the Gulf on whom Britain could count as an ally.

Wider calculations came into play. The UAE remains loyal to the Saudi crown prince, giving him a 21-gun salute when he visited Abu Dhabi this weekend. But the UAE was made aware that the Hedges affair could lead to it being put into the same authoritarian box as Saudi if it was not careful.

Facebook Twitter Pinterest The Saudi crown prince, Mohammed bin Salman, with Sheikh Mohamed bin Zayed al-Nahyan, the crown prince of Abu Dhabi and deputy supreme commander of the UAE armed forces. Photograph: Xinhua/Rex/Shutterstock

The only beneficiary of this, waiting in the wings, would be the UAE’s regional foe, Qatar, increasingly seen as a haven of normality in comparison with the security states of the UAE and Saudi Arabia. For example, Qatar’s foreign minister, Mohammed bin Abdulrahman al-Thani, was a noted guest at the Italian foreign ministry’s Mediterranean conference in Rome this weekend, sounding a lot more rational and balanced than some of the other voices from the Gulf.

But a route still needed to be found to extricate both sides from the crisis that had erupted. The search for a face-saving formula was on. The UAE needed to protect the integrity of the judicial system and the validity of the original espionage charge. Hunt simply wanted Hedges released and the relationship restored.

By happy coincidence, the UAE’s national day provided the perfect graceful exit. By tradition, Middle Eastern leaders, not just in the Gulf, use royal pardons to demonstrate their magnanimous wisdom. By coincidence, the UAE’s national day fell this week, and close to 800 pardons were set to be announced by the president, Sheikh Khalifa.

By last Friday it was clear, from speaking to both sides, that a formula was within touching distance and an announcement would be made at the beginning of this week.

A fear over the weekend was that Hedges’ wife, Daniela Tejada, in justified anger, might say something so provocative that the royal family would retract the pardon. Nevertheless, the FCO did not brief her precisely on what was afoot. Hunt spoke on Sunday to the UAE’s foreign minister, Sheikh Abdullah bin Zayed al-Nahyan, to confirm that everything was in place, and correspondents were tipped off.

Facebook Twitter Pinterest Daniela Tejada. Photograph: Ben Stansall/AFP/Getty Images

Hunt agreed that it would be possible for the UAE to defend the independence of its judicial system even if he could not acknowledge that Hedges was a spy, since he was not. The UAE stuck to its claim that Hedges was a spy in the pay of MI6. Honour was preserved on both sides.

But there will be an inquest in the FCO and the UAE as to how this crisis ever happened. For the FCO, whether to campaign in public for the release of British nationals and express general human rights concerns, or instead to use the diplomatic word in the ear, is an age-old dilemma. Hunt probably faces that dilemma with leaders of two-thirds of the globe that he meets, and he makes a country-by-country judgment. In China, for instance, he believes public campaigning will simply lead to the door being slammed in his face.

Yet Tejada clearly felt the British embassy in the UAE soft-pedalled to the point of immobility. It is said that it was only in the last few days that the UK really turned up the gas in private to convince the UAE that it had made a mistake. That was largely due to the pressure astutely applied via the media by Tejada.

Equally, the UAE will be challenged to review the mindset that led it to believe Hedges was a spy. The quality of the evidence, collected through interrogation and examination of Hedges’ computer, is apparently thin, and rarely goes beyond open-source evidence. The likelihood that Hedges, a non-Arabic speaker, was an M16 agent snooping on UAE intentions in Yemen is close to zero. UK military officers are in the control room in Riyadh advising on targeting strategy. The UK is a member of the Quint, the group that regularly meets with Saudi and the UAE to discuss strategy towards Yemen. UK intelligence officers are in regular contact with their opposite numbers in the Gulf.

The added value to British intelligence of the thoughts of a 31-year-old academic from Durham would be low. He may well have spoken to the British embassy about his research, and perhaps sometimes the interface between academic research and professional security consultancy could be better policed. That does not make him a spy.

Facebook Twitter Pinterest The British foreign secretary, Jeremy Hunt. Photograph: Andy Rain/EPA

The damaged UK-UAE relationship will take time to recover, and many on the left will redouble their criticism of British alliances with the authoritarian Gulf royal families in the Middle East. If a Labour government under Jeremy Corbyn was elected, the relationships would change radically.

But for the Conservative government there are mediating forces. UK troops are stationed at al-Minhad air base. UAE cadets train at Sandhurst each year. Arms and cyber sales are high. In terms of soft power, the tourist trade, and flow of academic exchanges, the relationship is equally rich. Figures such as Anwar Gargash, a UAE foreign minister, is a regular and lauded visitor to the FCO and Conservative thinktanks. Gargash was one of the first to call for the episode to be put behind behind them.

Hunt, for his part, can claim a personal success early in his foreign secretaryship, showing a lightness of touch that eluded his predecessor, Boris Johnson. Hunt cares about consular cases, admitting privately that the Welsh blood in him brought him close to tears in Tehran last week when he met the four-year-old daughter of Zaghari-Ratcliffe and she presented him with a present to give to his own child.

These consular cases require incredible tact as ministers negotiate the hazardous shoals of national interest, personal pain, conflicting family requests and an insatiable media.

Hunt may have been tepid initially in adopting the Hedges case, but thanks to him and Tejada’s campaigning, what could have been a very long march to freedom for Hedges has been made that much shorter.