The possibility of the UK leaving the European Union blared like an air-raid siren in Government Buildings, the quadrangle of Portland stone and Wicklow granite in central Dublin that houses the taoiseach’s office. Officials summoned politicians, diplomats, business leaders, farming groups, academics and others to the complex off Upper Merrion Street. The guests climbed a beechwood staircase with a stained glass window, My Four Green Fields, representing Ireland’s four provinces.

At the improvised summit all agreed that Britain’s exit from the EU would present an unprecedented threat to Irish interests. They agreed to meet monthly to brainstorm – discussions which led to a task force, a strategy, a plan.

This was 2015. Few people had even heard of the word Brexit. It was a year before David Cameron called the referendum, two years before Theresa May declared “red lines” over the UK’s withdrawal. It was three years before she agreed to the Irish border backstop as an option and signed the withdrawal agreement that triggered a breakdown in Westminster.

The roots of the UK’s political and constitutional crisis spread far and wide: decades of anti-EU propaganda, a Tory party civil war, a reckless Cameron gamble, a dogged, blinkered successor, a divided Labour party. And with a starring role, the backstop.

Protesters against a border between Ireland and Northern Ireland at the Carrickcarnan border. Photograph: Paul Faith/AFP/Getty Images

A term once confined to rounders and baseball, it refers to an insurance policy to avoid a hard border between Northern Ireland and the Republic of Ireland in the event of the UK’s no-deal departure. The Irish government persuaded the EU to make the backstop a condition of the UK’s withdrawal.

Supporters, including Tony Blair and John Major, say it is needed to protect the 1998 Good Friday agreement which drew a line under the Troubles and ended a three-decade conflict that claimed more than 3,600 lives. They say the return of border controls and infrastructure could wreck the peace process.

Brexiters call the backstop a trap which could chain the UK to Brussels in a de facto customs union and undermine the union between Great Britain and Northern Ireland.

Both sides agree that Dublin outplayed its former colonial master. While London dithered in the referendum aftermath, the Irish nimbly forged an alliance of all 27 member states.

“I pay tribute to the Irish government and Ireland the way that ... you know ... people in Dublin really saw the issues a long way out, much more clearly, perhaps than people in London did,” a rueful Boris Johnson, the former British foreign secretary, admitted in January during a visit to Dublin.

Enda Kenny addresses journalists following the result of the UK’s EU referendum. Photograph: Paulo Nunes Dos Santos/AFP/Getty Images

The story of the backstop is not of a flawlessly executed Celtic masterplan, however. Dublin conceived it fitfully and gradually in response to British contradictions and missteps. But once crystallised as a goal, the Irish pursued it hell for leather.

“It was very clearly identified at the very early stages that Northern Ireland and the border would become an issue,” Helen McEntee, Ireland’s minister of state for European affairs, said in an interview this week.

“The border came up at every meeting,” said Andrew Gilmore, deputy research director at the Institute of International and European Affairs thinktank, who attended stakeholder consultations.

While the UK suffered from what some call “Brexit comprehension lag” the Irish were quick off the blocks.

Enda Kenny, the then taoiseach, ordered the first Brexit impact study in 2014. Officials identified the impact on the economy, diplomatic ties, and crucially the Good Friday agreement and what Brexit would mean for the border. A further 85-page report was produced in November 2015 outlining consequences of a yes and a no vote.

Timeline Five key moments for the Irish backstop Show

It went into detail that barely troubled the leave campaign, including options for membership of the EEA, a customs union, bilateral trade agreements and the doomsday option of a no-deal exit, in which the UK and EU would default to World Trade Organization rules.

After the shock of the no vote sunk in, Kenny quickly assembled a team of almost 100 people to work on Brexit. He beefed up embassies across EU capitals to convey a stark message: the border was not just about protecting the single market, it was about peace.

Irish policymakers knew referendums could turn into opportunities for voters to kick the political establishment – it happened in Ireland numerous times. To neutralise the danger Kenny lobbied the EU to grant Cameron meaningful concessions in talks in early 2016 to help him win the referendum. He also warned Cameron to be extremely careful.

All in vain. The EU concessions to Cameron were meagre and he bungled the campaign.

“It’s often difficult to get information out to people, to make it clear what it is that you’re asking of them. So there was always a concern that it might not be as straightforward as people thought,” said McEntee, speaking from an office adorned with Irish and EU flags.

Despite their forebodings, the referendum result shocked Irish officials. They had hoped to shelve the contingency plans. Instead they scrambled to develop them.

“Those documents started as a basis and have evolved and grown legs and wings and developed into what we have now in terms of our strategy,” said McEntee.

The Irish initially hoped bilateral talks could find a way to keep the border frictionless. The Good Friday agreement had fostered close ties with London. British officials often admired Government Buildings, an Edwardian gem designed by Sir Aston Webb, who also designed Buckingham Palace.

But investigation of technical solutions, such as cameras or trusted traders, proved fruitless. In October 2016 Theresa May, who had replaced Cameron as prime minister, told the Tory party conference the UK would leave the single market, the first of her red lines.

As she was speaking to appease the right wing of the party and neuter Ukip support in the country, the Irish government was growing increasingly worried about the border.

In October Kenny agreed with the European commission president, Jean-Claude Juncker, that technical talks between officials in Brussels and Dublin would open. They met again in January 2017 and agreed “there was no technical solution” to the border.

They were beginning to realise that only a political solution – specifically, regulatory alignment between Northern Ireland and the EU – could avert a hard border. This was the genesis of the backstop.

That conviction grew when May gave her Lancaster House speech later in January, hardening her red lines, promising the UK would leave the customs union and the single market. Brexit did not mean Brexit; it meant hard Brexit.

In Brussels and Dublin it was decided that the UK now “had to own” the border issue.

All they had extracted out of Britain so far was a line in May’s speech that there would be “no return to the borders of the past”.

“That was no good to us,” said one source. “That referenced watchtowers and guns, but this is not what the border issue was about, we were never going back there. This was about the customs union and the single market.”

The turning point came a few months later in a keynote speech by Kenny, who for the first time indicated the bilateral approach was dead and avoiding a post-Brexit “hard border” was now vital for the republic’s national interest.

It was a shift in Ireland’s position, born out of the investigations into the border technical issues conducted with the commission over the previous five months.

The Irish goal was to get the border into a legally binding withdrawal agreement – Dublin’s point of maximum leverage – rather than future trade relations, when Dublin would struggle to be heard.

Rumours that Ireland was angling for a legally binding solution spooked the then Brexit secretary, David Davis. A scheduled meeting with Ireland’s deputy prime minister was mysteriously cancelled. “When they heard what was potentially to be discussed, he suddenly wasn’t available,” said a source. “The relationship with David Davis was not good.”

The Irish discounted Downing Street promises to keep the border as frictionless as possible, viewing the British as “slippery”, saying the right things but not necessarily delivering, said a source close to the talks. “They had decades of experience of negotiations with the British over Northern Ireland and knew what they would be like.”

Ireland was a midget with a big advantage: British ineptitude. Downing Street failed to plan for Brexit before the referendum and never caught up. Conservative party divisions split the cabinet and sabotaged attempts to clarify goals and strategy.

May compounded her difficulties by calling an election in June 2017 that lost her parliamentary majority and made her dependent on the Democratic Unionist party (DUP). The DUP opposed any special status for Northern Ireland lest it weaken the union. The prime minister tried to appease the DUP by widening the backstop to encompass the UK, not just Northern Ireland, but this angered Brexiters.

Downing Street’s road to perdition began with three incompatible commitments: leaving the single market and customs union, the UK jumping together, and no hard border in Ireland. Logic said you could have two, not three.

Brexiters squared this by saying any backstop would be a “convenient fiction” and not really legally binding. “Cakeism” – wanting to have your cake and eat it – entered the political lexicon. Ivan Rogers, the UK’s ambassador to Brussels, resigned in despair at “fantasies and delusions”.

After Leo Varadkar succeeded Kenny in June 2017 the Irish pressed their advantage. The new taoiseach wanted a “win”, said one Fine Gael party colleague, and squeezing the Brits proved popular. A government colleague said Varadkar, a policy wonk with a direct style, was simply pursuing the national interest. “He’s Mr Spock. He looks at what’s logical and tries to operate on that basis.”

Unlike May, besieged on all fronts, Varadkar enjoyed support from opposition parties and media commentators, who treated Brexit as a national emergency.

The taoiseach relied on seasoned diplomats – Rory Montgomery, Declan Kelleher, John Callinan – and a tightknit cabinet team. McEntee operated from his office. Simon Coveney, his deputy and foreign minister, operated from Iveagh House, a 10-minute walk across St Stephen’s Green. Phil Hogan, Ireland’s EU commissioner, provided intelligence from Brussels.

Britain assumed EU players would override Ireland. Surely German car makers and French farmers would not let fussing about a drizzly border between Derry and Dundalk imperil an orderly UK exit? But Berlin and Paris rebuffed Downing Street.

Leo Varadkar (right) with the EU Brexit chief negotiator, Michel Barnier in Dublin this month. Photograph: Clodagh Kilcoyne/AFP/Getty Images

Solidarity belonged to the member staying in the club, not the one leaving. Some had a personal stake in the backstop. Michel Barnier, the EU’s chief negotiator, used to be a commissioner who ran peace programmes in Northern Ireland. Baltic states with an eye on Russia appreciated the bloc supporting a minnow against a heavyweight.

Some EU members needed wooing. “There was a feeling that people in Europe had forgotten about Northern Ireland … we needed to re-educate them,” said a source.

Varadkar cited an Irish Times article about a 1972 IRA attack on a customs post. Ministers racked up air miles courting support and invited counterparts to visit the border.

The Irish government had a semi-permanent base camp at the Carrickdale hotel near the old border customs post at Jonesborough on the road between Dublin and Belfast. It hosted a revolving cast of officials, foreign affairs ministers and premiers including Xavier Bettel, Michel Barnier, and Donald Tusk.

It was vital to show that the border was an invisible line across fields and communities, said McEntee. “People see that and they understand how complex it is.” When former police officers brief them about the Troubles “it becomes much more real”, she said.

And so unfolded a historic reversal.

After the Anglo-Irish war in 1921 Dublin’s envoys bowed to British pressure and agreed to partition, creating a 310-mile border. Ireland’s ensuing civil war claimed the life of its chief negotiator, Michael Collins, but David Lloyd George claimed to have solved the Irish question.

Almost a century later, another British premier is tangled in a deal involving that same border. It has sundered her party and parliament. Her pleas to reopen the agreement have failed.

It is a great feat for Irish diplomacy. But Dublin is not celebrating. Unless the UK ratifies the withdrawal deal or cancels Brexit it may yet crash out of the EU with no deal, wreaking havoc on both sides of the Irish border and rendering the backstop a hollow victory. The Irish pay history some heed. They do not claim to have solved the British question.

• Tomorrow: Daniel Boffey on how Brussels outmanoeuvred the UK

• The caption to the picture of Leo Varadkar and Michel Barnier was amended on 18 April 2019 because an earlier version said Varadkhar was on the left. This has been corrected.