Devotees of the West Wing have been talking about it for weeks: the uncanny similarity between the fictional presidential contest that dominated the final seasons of the acclaimed TV show and the real-life drama of this year's election.

Both the real and imagined campaigns have centred on a young, charismatic candidate from an ethnic minority, daring to take on an establishment workhorse with a promise to transcend race and heal America's partisan divide.

But there's a twist.

For what those West Wing fans stunned by the similarity between the fictitious Matthew Santos and the real-life Barack Obama have not known is that the resemblance is no coincidence. When the West Wing scriptwriters first devised their fictitious presidential candidate in the late summer of 2004, they modelled him in part on a young Illinois politician - not yet even a US senator - by the name of Barack Obama.

"I drew inspiration from him in drawing this character," West Wing writer and producer Eli Attie told the Guardian. "When I had to write, Obama was just appearing on the national scene. He had done a great speech at the convention [which nominated John Kerry] and people were beginning to talk about him."

Attie, who served as chief speechwriter to Al Gore during the ill-fated 2000 campaign and who wrote many of the key Santos episodes of the West Wing, put in a call to Obama aide David Axelrod.

"I said, 'Tell me about this guy Barack Obama.'"

With the Latino actor Jimmy Smits already cast for the show, Attie was especially keen to know how rising star Obama approached the question of his race. Axelrod's answers helped inform Santos's approach to his own Hispanic identity.

"Some of Santos's insistence on not being defined by his race, his pride in it even as he rises above it, came from that," Attie said.

The scriptwriter also borrowed from Obama's life the notion of a superstar candidate. "After that convention speech, Obama's life changed. He was mobbed wherever he went. He was more than a candidate seeking votes: people were seeking him. Some of Santos's celebrity aura came from that."

The result is a bizarre case of art imitating life - only for life to imitate art back again.

In the TV show, Santos begins as the rank outsider up against a national figure famous for standing at the side of a popular Democratic president. There are doubts about Santos's inexperience, having served just a few years in Congress, and about his ability to persuade voters to back an ethnic minority candidate - even as his own ethnic group harbour suspicions that he might not identify with them sufficiently.

But the soaring power of his rhetoric, his declaration that the old divisions belong in the past and his sheer magnetism, ensure that he comes from behind in a fiercely close primary campaign and draws level with his once all-commanding opponent. Every aspect of that storyline has come true for Barack Obama. Axelrod, now chief strategist for the Obama campaign, recently joked in an email to Attie: "We're living your scripts!"

What's more, the West Wing had the Republicans choose between a Christian preacher - a pre-echo of Mike Huckabee - and an older, maverick senator from the American west whose liberal positions on some issues had earned the distrust of the party's conservative base: a dead ringer for John McCain.

In the West Wing, the McCain figure emerges comfortably as the party's choice. Apparently the character was not based on the current Republican frontrunner, but was simply a function of the casting of Alan Alda.

"It was always an inside joke on the West Wing that the show had a prophetic quality," recalls Attie, now a writer and producer of House, starring Hugh Laurie.

Various political scenarios sketched out on the programme would often materialise within weeks of airing. But the 2008 campaign, Attie concedes, is in an entirely different league.

There are small differences of course. Santos had a white wife - stressing, says Attie, Santos's standing as a "post-racial figure" - while Michelle Obama is African-American. Ms Obama is the more outspoken, but with two young children each, both are equally photogenic.

Obama aides will be hoping that the West Wing's prophetic streak holds: Santos eventually emerged as the Democratic nominee from a brokered convention - and went on to win the presidency.

Barack Obama v Matt Santos

Barack Obama

Young, handsome and charismatic member of Congress, attempts to become America's first non-white president.

Began political career as a community organiser in a big city (Chicago) before winning first election at local level. Married, with two young children.

Faced stiff opposition in Democratic primary against occupant of the White House during previous Democratic administration (first lady Hillary Clinton)

Rivals attack him as inexperienced after just four years in Congress, but triumphs through grassroots support, inspiring speeches and message of change.

Republican opponent is veteran moderate senator from a western state, unpopular with conservative base (John McCain of Arizona).

Matt Santos

Young, handsome and charismatic member of Congress, attempts to become America's first non-white president.

Began political career as a community organiser in a big city (Houston) before winning first election at local level. Married, with two young children.

Faced stiff opposition in Democratic primary against occupant of the White House during previous Democratic administration (vice president Bob Russell).

Rivals attack him as inexperienced after just six years in Congress, but triumphs through grassroots support, inspiring speeches and message of change.

Republican opponent was veteran moderate senator from a western state, unpopular with conservative base (Arnie Vinick of California).