There could be no doubt: someone had broken into my flat. Three months after arriving in Russia as the Guardian's new Moscow bureau chief, I returned home late from a dinner party. Everything appeared normal. Children's clothes lying in the corridor, books piled horizontally in the living room, the comforting debris of family life. And then I saw it. The window of my son's bedroom was wide open...

It wasn't open when I left five hours earlier, taking my children, Ruskin, six, and Tilly, nine, with me. We lived on the 10th floor of one of Moscow's post-communist-era apartment blocks, an ugly, orange-brick tower in the Moscow suburb of Voikovskaya. We kept our windows shut. The danger of a child falling out was too obvious.

To open the window, you had to twist the white plastic handle downwards 90 degrees. This was possible only from the inside; it couldn't have blown open. But the window was open, almost provocatively, defiantly so.

"Has there been a burglar?" my son asked, peering down at the frozen courtyard below. "I don't know. Perhaps someone managed to climb up the outside. Maybe it was Spiderman," I said weakly.

In the spare room I discovered a tape hissing in a music player. I hadn't put it on and my wife Phoebe had spent the night away with friends. Several hours later, while trying to suppress a feeling of horror, alarm, incredulity, bafflement and a kind of cold rational rage, I woke up. An unknown alarm clock had gone off somewhere in the flat. I went into the living room and turned on the lights. A clock was beeping loudly. I hadn't set it. But someone else had – to go off at 4.10am. I looked at the date; it was Sunday 29 April 2007.

It was clear that this was no orthodox burglary. They had apparently entered through the front door – the locks didn't seem to have troubled them. Nothing had been stolen; nothing damaged. The intruders' apparent aim had been merely to demonstrate that they had been there, and presumably to show that they could come back. The dark symbolism of the open window in the children's bedroom was not hard to decipher: take care, or your kids might just fall out. The men – I assume it was men – had vanished like ghosts.

I pretty much knew the identity of my ghosts – or the agency that had sent them. Fifteen days earlier, on 13 April 2007, the Russian oligarch and outspoken Kremlin critic Boris Berezovsky had given an interview to my newspaper in which he called for the violent overthrow of Vladimir Putin's regime. Since then, Russia's successor agency to the KGB had taken a keen interest in me.

After the end of communism and the Soviet Union, the KGB had got a new name. It was now the Federal Security Bureau, or FSB. This was the main domestic state-security organisation. Its job was to carry out counter-espionage. Apparently that meant me. My name had been on the Guardian's front-page story, together with that of two London-based colleagues. Within hours of the Berezovsky scoop, my new strange life began.

Someone hacked into my private email account. A person claiming to be from the "president's office" called my office and demanded my mobile phone number. I didn't give it to them. A middle-aged woman, casually dressed and with – I noted – a rather bad 1970s-ish haircut, appeared outside my front door at 7am. When I opened it, she just examined me and left.

Two days after publication, I flew from Moscow to London on Aeroflot for a family funeral. I'd gone through the last security gate when someone slapped me, hard, on the shoulder. I turned round. There was a young man, wearing a leather jacket – the unmistakable uniform of the KGB spook. He was smirking. "There is something wrong with your jacket," he said in a strong Russian accent.

After takeoff, I made my way to the loo. I took off my jacket and shirt. There was nothing on them. But then, I wouldn't actually know what a bugging device looked like, I reflected.

In the pastiche neo-Soviet Russia that Putin had created since becoming president, the FSB had become the pre-eminent power in the land – a huge, secret, prodigiously resourced organisation that operated outside the framework of the law, according to its own set of (also secret) rules. The FSB felt empowered to crush anyone it considered enemies of the state. This meant Russia's tiny and demoralised band of opposition politicians. It meant human rights activists; workers for foreign NGOs; and businessmen who failed to observe the regime's new rules – obey the state and stay out of politics. It meant foreign diplomats, especially British ones. It also appeared to mean troublesome western journalists. Most dangerously, though, it meant traitors.

It was clear to me – and apparently to the British government as well – that there was an FSB dimension to the murder in London of the Russian dissident Alexander Litvinenko. He had died in a London hospital in November 2006, three weeks after sipping a cup of green tea poisoned with radioactive polonium-210. Litvinenko was a former FSB officer. So was his alleged killer. The man who had apparently slipped him the cup was Andrei Lugovoi. But the identity of the person who had sent the killer remained a mystery. Was it, as Litvinenko's friends and family alleged, Putin? Or were other dark forces inside the Kremlin responsible, keen to provoke a crisis between Moscow and the west?

By the time of Litvinenko's murder, former KGB agents – a hardline group known as siloviki – had risen to key positions inside Putin's Kremlin. In 1999, Putin had become the head of the FSB. In 2000 he was elected as president and quickly elevated trusted members of the security services into the governerships of Russia's provinces, into ministries and into the directorships of state-owned companies. The KGB were back.

Sociologists estimated that in 2003 the number of senior Kremlin officials with a security/military background was 25%. By 2006 the figure for "affiliated" siloviki – including both official and unofficial agents – was an astonishing 77%. The siloviki viewed the demise of the Soviet Union as a humiliating disaster. Their mission – as they saw it – was to restore Russia's lost greatness. Breaking into people's flats, meanwhile, was an old KGB technique, designed to intimidate and harass, rather than to kill. If the person complained to the police, officers would merely politely respond with a suggestion of paranoia. After all, how do you complain about a break-in when the culprits are working for the state?

Three weeks after the Berezovsky interview, I received a phone call. It was the FSB. The story had caused a furore inside Russia, even pricking politicians from Russia's normally horizontal state duma to demand Berezovsky's extradition from Britain, something Britain's judicial system had consistently refused to do. In May 2007, the FSB launched its own criminal investigation into our story. Russia's chief prosecutor, Yuri Chaika, had already charged Berezovsky with fraud – accusing him of stealing £4.3m from Aeroflot – but it was clear that additional criminal charges would bolster the prosecutor's case and might just embarrass the British government. The FSB officer, who didn't identify himself, was polite but adamant: "You have to come and see us," he said. My own role in the Berezovsky story had been modest, I said. I had merely phoned the Kremlin's urbane, English-speaking spokesman, Dmitry Peskov, and asked if he had a reaction. It failed to deflect the FSB man. "I suggest you bring a lawyer," he said.

Three weeks after the phone call, I found myself with my lawyer, Gari Mirzoyan, a veteran of Moscow's criminal circuit, outside Lefortovo prison, a drab, three-storey building lined with spiralling razor wire close to the centre of Moscow. In communist times, Lefortovo was the KGB's most notorious jail, and not a place to which journalists were normally admitted, especially foreign ones. A large reinforced metal door swung open. Inside was a small reception area. The reception itself was hidden by a one-way silvered mirror: the officer on duty could see us; we could not see him. A disembodied hand appeared briefly; it took away my passport and phone. We were given permission to proceed upstairs. The lift had old-fashioned prison bars; it was, in effect, a moving cage. It appeared to descend to Lefortovo's K-shaped internal prison, where a small number of detainees, mostly political prisoners, were still kept. Old-fashioned video cameras recorded our movements from the stairwells; the corridor was lined with identical, anonymous wooden doors. We arrived at room 306 and knocked.

Major AV Kuzmin answered. To my surprise, he was a young man – 29 or 30, perhaps – wearing a dark olive-green FSB uniform. The fact that he was investigating the Berezovsky case – at the behest of the presidential administration – suggested he was already moving rapidly up the FSB's career ladder. On his desk was a colour photocopy of the Guardian's Berezovsky front page. He tossed it to me. "Could you confirm who you are?" he asked.

"Luke Daniel Harding," I said.

"How long have you been in Moscow?"

"Three months."

"Can you tell me the circumstances in which your interview with Berezovsky took place?"

"It took place in London."

"How do you know this?"

"The Guardian's legal department told me."

And so on. At first it seemed this wasn't really an interrogation but a piece of bureaucratic book-keeping. Later, I realised the aim wasn't to unravel the truth; it was to intimidate me. Kuzmin knew my answers in advance. By this point, the FSB had apparently broken into my flat, bugged my phone and hacked into my email; there wasn't much that would have surprised them. After 55 minutes, Kuzmin declared our interview over. I signed my witness statement. I wanted a drink, but had declined the fizzy water, fearing – unreasonably, I am sure – that it may have been tampered with.

The FSB's invisible presence continued; the agency became an intangible part of my Moscow life – sometimes loudly, sometimes quietly, with someone in a back room clearly turning the volume of minor persecution up and down. That someone listened to my phone calls was made clear most days. FSB agents cut the line whenever my conversation strayed into sensitive areas. Saying words such as "Berezovsky" or "Litvinenko" meant the immediate end of any call. (For a while, I substituted the word "banana" for Berezovsky. Amazingly, this appeared to work.) Discussions of Kremlin politics also ended badly, with the frustrating beep-beep of a disconnected line.

In early December 2007, I arranged a meeting with Olga Kryshtanovskaya, Russia's foremost expert on Kremlin elites and a researcher at the Institute of Sociology at the Academy of Sciences. Sitting in her living room, clad in a pair of her guest slippers, I asked about the FSB's methods. They maintained a listening station somewhere in Podmoskovi – Moscow's suburbs – she said. Its existence was a state secret. The FSB had its own special department for spying on foreign diplomats, she added; it probably had one for watching foreign journalists as well. The listeners were told whom they had to listen to. Wasn't this rather boring work? "The thing that keeps them going is the idea that they are serving their country and defeating its enemies," she said. Those who had worked in intelligence gathering – including Putin and Sergei Ivanov, Russia's hawkish former defence minister – tended to be brighter and more flexible. The most fanatical hardliners came from counter-intelligence, she suggested, characterising them as zombies. "These people were brought up in the Soviet Union. They were super-isolationist. They didn't know anything about the west. They were fed zombie propaganda and ended up as orthodox fanatics."

Membership of this most secret of clubs offered certain benefits – benefits that compensated for the relatively derisory level of pay. "If you work for the FSB, you don't have to worry about the law. You can kill someone and nothing will happen," Kryshtanovskaya said. I asked about the murder of Litvinenko. Senior officers in the FSB had privately admitted to her that his assassination must have been an FSB operation, she said. They had no regrets about the target – a traitor to Russia and someone who deserved to be murdered – but were unimpressed about the bungling and messy way his assassination had been carried out.

On her doorstep, I gave her back the guest slippers. She gave me a word of advice. "Take care," she said. Why? "Because you are an enemy of Putin," she replied, matter-of-factly.

By August 2008, we had left Voikovskaya and moved into a wooden dacha in the artists' colony of Sokol in north-west Moscow. The house was a haven amid the madness of the city: lily of the valley grew near our front gate, Virginia creeper decked the green picket fence. The cycle of harassment from the FSB had seemingly tailed off. And then, on 7 August, war erupted in Georgia. The flashpoint was the separatist enclave of South Ossetia, run in effect by Moscow and the FSB. Over the following three weeks, I reported what I witnessed as Russian columns swept down from South Ossetia's capital, Tskhinvali, towards Tbilisi. The Georgian army's disastrous incursion into Tskhinvali itself had been crushed; what was left of president Mikhail Saakashvili's forces was in disarray and retreat. In the Georgian border villages next to South Ossetia, South Ossetian irregulars went on a murderous rampage. Backed by the Russian army, they shot dead ethnic Georgian civilians (teenage boys first), looted cars and household furniture, and torched Georgian houses. It was 21st-century ethnic cleansing, and I said as much. These killings by Russian proxies went unreported inside Russia itself. Instead, state-controlled TV hailed Moscow's long-planned invasion of Georgia as a peacekeeping operation.

When I got back to Moscow, the mood towards western journalists was sour and vengeful. On 25 November 2008, I had an unhappy meeting with Boris Shardakov, the foreign ministry official responsible for British journalists' accreditation. "Why do you stay in this country?" he said. "Is your family not afraid that if you remain here something unpleasant might happen to you?" Was this a threat? It looked like one.

At the same time, the FSB resumed and escalated its campaign of brutishness. The break-ins at my home and flat became numerous. I kept a log.

29 October-2 November 2008. Upper outer right bedroom window open. Shut when we left. Batteries removed from alarm system in every room in the house.

8 December 2008. Central heating disconnected. House freezing. Mobile-like ringing from under the stairs in middle of the night. Can't find source. Ringing continues.

'The FSB’s invisible presence continued. I kept a log. 30 January 2009. Break-in at office. Screensaver of Phoebe and the kids deleted from computer'

30 January 2009. Break-in at Guardian office. Screensaver showing Phoebe and kids deleted from my computer. Screen locked. Keyboard wiped clean. Door and lock stiff.

3 February 2009. Email to British Embassy returns with message deleted and "NULL" written on it.

By this point it was clear the security services were prepared to continue their campaign until I got the message and cleared off. The break-ins were not without humour. I once found a cheap paperback left by the side of my bed, offering tips on how to achieve better orgasms.

On 30 June 2010, the FSB broke into my office again. They unplugged the internet, opened the window and left the phone off the hook, placing it next to my laptop. The message was clear: we are still here. That day I had written a story about Anna Chapman, the spy who was part of a network of sleeper Russian agents uncovered in the US. The nocturnal visit was a reminder that I was again pursuing themes the Kremlin considers off-limits. Most news organisations in Russia – including, despite the efforts of its braver correspondents, the BBC – obey a series of informal rules, all of which I had broken. Taboo themes include corruption in the Kremlin, activities of Russia's intelligence agencies and human rights abuses by federal security forces and their local proxies in the troubled North Caucasus, and speculation about Putin's personal wealth, which some sources say is as much as $40bn. US diplomatic cables alleged "secret assets" abroad. Putin has denied this.

My Russian accreditation and visa expired on 27 November 2010; as in previous years, I had asked the foreign ministry for a renewal. The phone call, on Tuesday 2 November 2010, is unexpected. I'm in London, immersed in reading the secret WikiLeaks cables on Russia. The Russian foreign ministry summons me to an urgent meeting. They refuse to explain what it is about. I return to Russia and on Tuesday 16 November I turn up at the ministry's press department. I am told I broke permit rules during a Greenpeace press tour 13 months earlier to Russia's Arctic and during a visit to Ingushetia in March 2010. As a result, my accreditation will no longer be renewed. The FSB is behind the decision. I point out that other journalists from Reuters and AFP didn't have the right paperwork on both trips. I ask if president Medvedev – apparently keen to modernise Russia and attract foreign investors – is aware of my expulsion? There are no answers to these questions. But the calm demeanour of Oleg Churilov, head of the press department, during this exchange makes me think that the order to deport me has come from the very top. I warn him there will be a scandal. He appears not to care.

My four years in Russia end, then, in dramatic fashion: with a textbook Soviet-style expulsion. I am the first western staff correspondent to suffer this fate since the end of the cold war. I'm stunned. But my expulsion is not, I reflect, a surprise. It's something I have always accepted as a real, if far-fetched, possibility. Western correspondents in Moscow meet at least once a month in informal gatherings known as the "hack pack". Six months earlier, a young woman doing an internship at the ministry of foreign affairs turned up at hack pack drinks. Asked which journalist the ministry hated most, she unwittingly replied: "There's a guy called Luke Harding – they really hate him."

'The Russian foreign ministry summons me to an urgent meeting. My visa will not be renewed. I am stunned.'

My family and I book tickets to leave Moscow on Wednesday 24 November. Twenty-four hours before our departure, my phone rings. It is Nikolai, the junior press department diplomat. "Mr Harding, I have good news for you," he says. "We are willing to give you a visa for six months, so your children can finish school."

It appears I am to be temporarily un-expelled, before being re-expelled later.

The reasons are unfathomable. This could be a pragmatic victory for the Kremlin's liberals. It's also possible that British diplomacy has done the trick. It's only later I reflect that the climbdown may always have been the plan. The FSB's decision has turned our life as a family upside down.

Phoebe, Tilly and Ruskin return to Russia in early January, so the children can resume school; I stay behind in Britain to finish a book about WikiLeaks. In the meantime, the Guardian publishes hundreds of stories based on the US state department's secret cables that see Russia depicted as a "mafia state". They are not happy reading for the Kremlin. My byline is on the stories.

On Saturday 5 February, British Midland flight 891 makes its approach to Moscow's Domodedovo international airport. I feel an unmistakable sinking in my stomach. On landing, I hand over my battered British passport. A federal border agency official taps in my details. She calls over her boss. They exchange glances, and then break into an embarrassed giggle. (I've observed this on previous occasions and wonder if something puerile, something mockingly unpleasant, is written on the agency's system next to my name – the bearer of this passport has a small cock?) I'm told to stand to one side. The supervisor takes my passport. After a few minutes another official, Nikolai, arrives.

Before I can ask what's going on, Nikolai launches into a brief speech: "In accordance with paragraph 27 of Russian federal law, you are refused entry to the Russian Federation," he says. Why, I ask. "For you, Russia is closed," he answers.

I text Phoebe: "I'm being deported." "NO," she texts back. I assure her that this isn't a joke.

Nikolai takes me through my own security control point, back to departures and gate number one. I realise I'm being sent back on the same British Midland flight I've just arrived on.

The FSB's decision to deport me causes a minor international scandal. After a career writing the news, I become the news. I am the subject of a debate in the House of Commons. I worry about my family stuck in Moscow.

It becomes apparent that the Russian foreign ministry is entirely clueless about my expulsion. Sources protest they know nothing of the FSB decision to put me on a blacklist. Putin's spokesman, Dmitry Peskov, denies the prime minister has anything to do with it. Exactly who took the decision, then, is a mystery. It is, nevertheless, embarrassing for the authorities. Within four days of my deportation, the Russian foreign ministry performs an extraordinary U-turn and gets in touch to say I can have a visa. Exactly a week after I'm deported, I return. My accreditation expires on 31 May 2011, I read – so the decision to grant me a new visa is merely an interim face-saving measure. In three months, when the scandal is quietly forgotten, I have to leave Russia again.

Back at our Moscow dacha, and reunited after more than a month apart, my family vote is 3-1 in favour of getting out. Only Phoebe votes to stay put. She has spent four years writing about the other Russia. While I've been immersed in the grim world of Kremlin politics, Phoebe has been roaming around Moscow under an open sky.

Back in England, I immediately deadlock the front door. In cafes and restaurants I glance over my shoulder, on the lookout for young men wearing cheap, ill-fitting suits and brown shoes. Once, I hear Russian voices outside on the street and find myself following two men. But over time, it appears that the old world has gone for good. When I return to the house, the white patio doors – bolted when I left – are still bolted. Household objects remain where I left them. We are anonymous again. And – I think – safe.

• Luke Harding's book, Mafia State: How One Reporter Became An Enemy Of The Brutal New Russia, is published on 29 September by Guardian Books at £20; ebook and audiobook editions are also available. To order a copy for £13 (including UK mainland p&p), go to guardianbookshop.co.uk