The year is 1984 and a genocidal Margaret Thatcher wants to replace the human race with time-travelling aliens from another dimension. There is only one person who can help: not Neil Kinnock, not Arthur Scargill, but Doctor Who.

Travelling in his infinitely accommodating Tardis, the Time Lord has vanquished the Daleks and dispatched the universe's most evil tyrants. But now, in a special story marking his 50th anniversary, Matt Smith's Doctor comes face to face with the Kin, an ancient foe of the Time Lords, which have adopted the persona of the late prime minister and now plan to rid the planet of humans.

In the story written by Neil Gaiman, the alien "Thatcher" has persuaded British homeowners to sell their properties at a vast profit under a plan to rid the planet of humans and replace them with the sinister Kin, an ancient time-travelling enemy of the Time Lords. The Doctor is tasked with rescuing humanity from her devilish plotting.

Download the new Independent Premium app Sharing the full story, not just the headlines

While left-wing scriptwriters are reported to have inserted anti-Thatcher storylines in the series during the 1980s, according to former Doctor Sylvester McCoy, the new tale is the first official meeting of the two cultural titans.

"Nothing O'Clock" has been written for a Penguin anthology of new short stories featuring all 11 Doctors. Gaiman, the best-selling author who has written two Matt Smith episodes for the BBC series, said he wanted to create "a creepy Doctor Who monster of the kind that we haven't quite seen before".

The creator of the Sandman graphic novel series added: "I wanted to see if I can scare people. I got to do lots of really interesting things including have the voice of the late Margaret Thatcher.

"She's there in 1984 and it was really a strange discovery that actually there's nothing quite as scary as a classic Doctor Who villain with Margaret Thatcher's speech patterns."

In the story, published this week, the fictional Thatcher – in reality the Kin wearing a mask – introduces herself to the Doctor in a familiar "breathy female voice". "You do know who we are, dear?' she asks with familiar menace. "It would be such a shame if you didn't."

Because the Kin has adopted the Thatcher guise, the Doctor says, people "are going to be much more willing to sell big important things, places that belong to the country, not to an individual, when they believe that the leader of their country is asking for them, personally". "Mrs Thatcher" explains that they will create "reservations" for the displaced humans but "they will die out. Well, dear … it won't be pretty". The Doctor rips off the Thatcher mask to reveal a face that "writhed and squirmed".

Readers can discover whether the Doctor foils the alien Thatcher master plan when Gaiman's story is released as an ebook download on Thursday. A physical anthology, including 10 other Doctor Who stories written by authors including Charlie Higson and Children's Laureate Malorie Blackman, will also be published.

Gaiman would like to see a television adaptation of the Thatcher/Doctor face-off. "There was definitely part of me who went, 'You know, wouldn't it be fun to put this on screen?'" he said. "Wouldn't this be fun to see if I can actually make people wee themselves with terror in real life?"

During the 1980s, a Doctor Who television story, The Happiness Patrol, featured a caricature of Thatcher, called Helen A, a big-haired despotic ruler of a human colony on the planet Terra Alpha, played by Sheila Hancock.

Andrew Cartmel, a former Doctor Who scriptwriter who once inserted a speech based on CND material in an episode, wrote a spin-off children's novel, which featured a villain called Rehctaht – Thatcher spelt backwards.

Sylvester McCoy said: "We were a group of politically motivated people and it seemed the right thing to do. Our feeling was that Margaret Thatcher was far more terrifying than any monster the Doctor had encountered."

The BBC marks the 50th anniversary next Saturday with a 75-minute special, The Day of the Doctor, starring Matt Smith, David Tennant, John Hurt and Billie Piper.