It's nearly 40 years since the Rolling Stones fled to the French Riviera and recorded their masterpiece, Exile on Main St. On the eve of its relaunch, Sean O'Hagan marvels that the album was made at all…

The following correction was printed in the Observer's For the record column, Sunday 9 May 2010

In this article we said Robert Greenfield in his book subtitled "A season in hell with the Rolling Stones" had aired a rumour "which he did not confirm or refute" that Anita Pallenberg had encouraged the teenage daughter of an employee to inject heroin for the first time. We should make it clear that the book discounts the rumour, stating that the employee, who was motivated by a desire to blackmail Pallenberg, did not even have a daughter. We reported that another rumour in the book "has Jagger bedding Pallenberg while Richards has nodded out on heroin". The book substantiates the rumour of the affair, but does not connect it to Richards's heroin addiction. We apologise for these errors.

There is a great moment in Stones in Exile, a new documentary about the making of Exile on Main St in 1971, when Keith Richards defines the essential difference in temperament between Mick Jagger and himself.

"Mick needs to know what he's going to do tomorrow," says Richards, his voice slurring into a laugh. "Me, I'm just happy to wake up and see who's hanging around. Mick's rock, I'm roll."

On Exile on Main St, though, Jagger, for once, rolled with Richards. So, too, did everyone else involved, from Jimmy Miller, the producer, to Marshall Chess, the young Atlantic Records executive, to the rest of the group and their extended retinue of session players, studio technicians and hangers-on.

Once the decision had been made to record the album in the basement of Villa Nellcôte, Richards's rented house in the south of France, the working schedule was dictated by the irregular hours kept by the group's wayward guitarist, who also had a singularly dogged approach to composing songs.

"A lot of Exile was done how Keith works," confirms Charlie Watts in the documentary, "which is, play it 20 times, marinade, play it another 20 times. He knows what he likes, but he's very loose." Without a trace of irony, Watts adds, "Keith's a very bohemian and eccentric person, he really is."

Exile on Main St is so emphatically stamped with Keith Richards's rock'n'roll signature that it could just as easily have been called "Torn and Frayed" after one of the two gloriously ragged songs that he wrote the lyrics for. The title alone sums up his gypsy demeanour, his elegantly wasted look. Or they could simply have called it "Happy", after another track that was actually recorded in a single take when Richards woke up one morning – or evening – and gathered up the only other people who were awake, saxophonist Bobby Keys and producer Jimmy Miller, who was drafted in to play drums in place of the absent Watts. The whole record was, says Keys, a good ol' boy from Texas, "about as unrehearsed as a hiccup".

Perhaps because he was not the controlling presence on Exile on Main St, which has often been voted the greatest rock'n'roll record ever by music critics, it is not necessarily one of Mick Jagger's favourite Rolling Stones albums. He once described it as sounding "lousy" with "no concerted effort of intention", adding "at the time, Jimmy Miller was not functioning properly. I had to finish the whole record myself, because otherwise there were just these drunks and junkies."

Jagger may have been miffed that his vocals are sometimes swallowed up in the soupy mix but he sings with real passion throughout and seems galvanised by the raw rock'n'roll the group are making. If anyone should need a reminder that no one before or since has sounded as louche and limber, so raggedly majestic, they should watch the Stones playing "Loving Cup" live on their subsequent American tour. Footage of that performance is a highlight of the documentary, produced by the Oscar -winning film-maker John Battsek, which will be premiered at the Cannes film festival before screening on the BBC later in May.

Despite his former reservations, Jagger has gotten behind the planned reissue of the album, too, which comes in a deluxe package containing 10 previously unheard bonus tracks, some of which are alternative takes of familiar songs while others sound suspiciously like they have only recently had new vocals added. No one in the Stones' camp is coming clean as to whether this is the case or not.

For the purists among us, though, the original version of Exile on Main St, in all its ragged, full-on, rock'n'roll swagger, is all we need. "This is just a tree of life," said Tom Waits, when he selected it as one of his all-time favourite records a few years back. "This record is a watering hole." On the documentary, Caleb Followill from Kings of Leon is taken aback to discover the album was recorded in France. "I literally thought they were in Memphis, going out every night eating barbecue and partying." Which is exactly what it sounds like.

The creation of Exile on Main St, like so many early chapters in the Rolling Stones story, is shrouded in myth and blurred by conflicting anecdotal evidence. The American journalist Robert Greenfield, who was present briefly during the recording, wrote an entire book about — and named after — the album. Its subtitle is "A Season in Hell With the Rolling Stones". The book paints an often lurid portrait of Richards and his then partner, Anita Pallenberg. Greenfield places the couple at the centre of a spiral of sustained hard drug abuse and wilfully amoral behaviour. Among the rumours he airs, but does not confirm or refute, is the one about Pallenberg encouraging an employee's young daughter to inject heroin for the first time. Another has Jagger bedding Pallenberg while Richards has nodded out on heroin, thus reigniting an affair they were rumoured to have had while filming Performance under the direction of Nic Roeg in 1968.

Needless to say, the documentary, which has Jagger's controlling presence written all over it, does not dwell on such unsavoury and unsubstantiated matters. The French photographer Dominique Tarle, who chronicled the making of the album in a series of wonderfully evocative shots, and who was Greenfield's entrée into the Stones' milieu, had this to say about the book when I spoke to him in Paris last week: "I read only eight pages and I really felt sick. First of all, how can he not write about the music? And all this stuff about a season in hell with the Rolling Stones? No, no, it was anything but that. We were all young and it was a time of great freedom and energy and creativity. For me, it was a kind of rock'n'roll heaven."

Perhaps, though, it was both. Tommy Weber, who is described as "a racing driver, drug runner and adventurer" in the documentary, and as "a fabulous character straight out of F Scott Fitzgerald's Tender is the Night" by Greenfield, was one of Richards's inner circle at Nellcôte. His son, Jake, now a Hollywood actor, was just eight when he witnessed the decadence around the Rolling Stones first-hand. In Stones in Exile, he says, "There was cocaine, a lot of joints. If you're living a decadent life, there is always darkness there. But, at this point, this was the moment of grace. This was before the darkness, the sunrise before the sunset."

Bobby Keys, as ever, is more blunt. "Hell, yeah, there was some pot around, there was some whiskey bottles around, there was scantily clad women. Hell, it was rock'n'roll!"

Others experienced more mundane but no less pressing problems. Both Charlie Watts and Bill Wyman missed home and some of their own creature comforts. "I hated leaving England," Wyman reminisces. "You had to import Bird's custard, Branston pickle and piccalilli... you had to buy PG Tips and then deal with the French milk."

The Rolling Stones pitched up in the south of France in the spring of 1971 as reluctant tax exiles fleeing the Labour government's punitive 93% tax on high earners. The group had just extricated themselves, at some cost, from a misguided management deal with the infamous Allen Klein, who was still claiming he owned their publishing rights. In the public eye, though, the Stones were still the rock group that most defined the outlaw rock'n'roll lifestyle, their bad reputation built on an already colourful past that included high-profile drug busts, the death by drowning of Brian Jones, one of their founding members, the near death by overdose of Marianne Faithfull, Mick Jagger's former girlfriend, and the murder of a fan by Hell's Angels, who had been hired by the group's management to provide security at 1969's ill-fated Altamont festival.

Altamont was viewed by many contemporary observers as the symbolic death of the 60s dream of a burgeoning counterculture; by others as an inevitable result of the Stones' hubris and arrogance. Through it all, though, the Stones' music had echoed their turbulent lifestyle and soundtracked the tumultuous times, from the upfront sexual bravado of "(I Can't Get No) Satisfaction" in 1965, through the apocalyptic swirl of "Gimme Shelter" in 1969, to the swagger of "Brown Sugar" in 1971.

Sticky Fingers, the group's ninth album, nestled at the top of the British and US pop charts as the Stones, their families and extended entourage decamped to France to begin their exile. Richards sensed that the reason for their flight from Britain was not just to do with their dire financial predicament.

"There was a feeling you were being edged out of your own country by the British government," he remembers. "They couldn't ignore that we were a force to be reckoned with."

Having searched the coastline and hills around the town of Villefranche-sur-Mer for a suitable recording space, the Stones then opted to start working in the cavernous, multi-roomed basement of Nellcôte, with their mobile recording studio parked outside in the driveway. The house had once been occupied by the Nazis, and in a recent interview Richards describes working there as "like trying to make a record in the Führerbunker. It was that sort of feeling… very Germanic down there – swastikas on the staircase… Upstairs, it was fantastic. Like Versailles. But down there… it was Dante's Inferno."

In the often intense heat of the dank basement, the group struggled to get started. Musicians set up their instruments in adjoining rooms, with Bill Wyman having to play his bass in one space while his amplifiers stood in a hallway. Initially, they were hampered by guitars going out of tune due to the humidity. Basic communication, too, was a problem, with Jimmy Miller continually having to run from the mobile studio to the basement to deliver his instructions.

Then, a few weeks in, Mick Jagger announced that he was going to marry Bianca Pérez Morena de Macias, a Nicaraguan-born model, in nearby St Tropez. The international press and a clutch of the world's most famous pop stars jetted in for the very public wedding ceremony. As Jagger and his bride departed on honeymoon, the celebrations continued for a week at Villa Nellcôte. A week after they stopped, Gram Parsons, the country-rock singer who had bonded with Richards in Los Angeles a few years before over their shared love for Merle Haggard and heroin, arrived with his wife, Gretchen. The couple stayed for a month before they were diplomatically asked to leave by a Stones minion. "The atmosphere kept changing but the party kept going," says Tarle, laughing.

Interestingly, the Stones in Exile documentary does not even mention Parsons, whose closeness to Richards rattled the possessive Jagger. "Keith and Gram were intimate like brothers," says Tarle, "especially musically. The idea was floating around that Gram would produce a Gram Parsons album for the newly formed Rolling Stones Records. Mick, I think, was a little afraid because that would mean that Gram and Keith might even tour together to promote it. And if there is no room for Mick, there is no room also for the Rolling Stones. So, yes, there was tension. You could feel it and I captured it on Mick's face in some of my pictures."

The music the Stones made in Nellcôte reflected those tensions, as well as the sense of exile and uncertainty that hung heavily over the group, and the continuing encroachment of heroin on the lives of Richards and Pallenberg, and on the lives of some of those who entered their orbit. Speaking recently, Richards protested that he was not the only drug user in the group. "At the time, Mick was taking everything. Charlie was hitting the brandy like a motherfucker. The least of our concerns was what we ingested. These sorts of questions [about drugs] are predicated on what came a few years later when… I would play the game. 'Oh, you want that Keith Richards? I'll give you the baddest mother you've ever seen.'"

By October, though, heroin use seems to have been a constant in the lives of Richards and Pallenberg. "I walked into the living room one day and this guy had a big bag of smack," Pallenberg remembers, "and everything just disintegrated." Perhaps it was telling that when Richards bought himself a speedboat, he called it Mandrax.

Heroin brought with it the usual problems of supply and demand, and the usual retinue of shady characters and criminals, both local and from nearby Marseille. Villa Nellcôte was such an open house that, one day in September, burglars walked out of the front gate with nine of Richards's guitars, Bobby Keys's saxophone and Bill Wyman's bass in broad daylight while the occupants were watching television in the living room. "That's how loose and stupid it was out there," says Wyman. The crime was reputedly carried out by dealers from Marseille who were owed money by Richards. The nocturnal goings-on at Nellcôte were also starting to attract the attention of the local populace and the increasingly suspicious police force. "The music was so loud, really, really loud," Pallenberg remembers. "Sometimes I went to Villefranche during the day and you could hear the music there. And it went on all night."

Whatever the truth of the rumour about Pallenberg encouraging the teenage daughter of the resident chef to try heroin, the police eventually raided Nellcôte and, in 1973, both she and Richards were charged with possession of heroin and intent to traffic. The resulting guilty verdict meant that Richards was banned from entering France for two years, and thus the Stones could not play concerts there.

As summer turned to autumn, people started drifting away from Nellcôte and, in November 1971, Richards and Pallenberg followed suit. The album was eventually finished in Sunset Sound studios in Los Angeles. In the documentary, Jagger reveals that some of the lyrics were written at the last minute, including the album's first single, "Tumbling Dice", which was composed "after I sat down with the housekeeper and talked about gambling". The words to another gambling song, the frenetic "Casino Boogie", were created by Jagger and Richards in the cut-up mode made famous by William Burroughs, which gives a lie to the notion that the line about "kissing cunt in Cannes" refers to an episode in Jagger's notoriously promiscuous sex life.

Jagger also denied recently that "Soul Survivor" was about his relationship with Keith Richards during the making of Exile. On it, he sings the line, "You're gonna be the death of me".

In places, Exile on Main St does indeed sound, in the best possible way, like an album made by a bunch of drunks and junkies who were somehow firing on all engines. Jim Price and Bobby Keys's horns are an integral part of the dirty sound, as is Nicky Hopkins's rolling piano. Songs such as the galloping opener, "Rocks Off", surely about the effects of a heroin hit, and "All Down the Line" are messily powerful, with vocals fading in and out of focus and the group kicking up a storm underneath. "Tumbling Dice" features one of the greatest opening gear changes in rock'n'roll and a swagger that carries all before it.

In one way, the double album, housed in Robert Frank's contact sheet-style cover, is Keith Richards's swan song of sorts, a final blast of rock'n'roll energy before he descended into a protracted heroin addiction that would often make him seem – and sound – disconnected from the rest of the group during live shows. After Exile, Jagger carried the weight and, despite some great moments on subsequent albums including Goat's Head Soup and Black and Blue, the Stones would never sound so sexy, so raucous and abandoned, so low-down and dirty. Neither, though, would anyone else. By the time punk came and went and indie rock had taken hold, the mix of sexiness and sassiness that the Stones at their best epitomised had disappeared entirely from rock music. So had the kind of survival instinct that the group drew on when the going got tough.

"The Stones really felt like exiles," Richards says. "It was us against the world now. So, fuck you! That was the attitude." You can still hear it, loud and clear, on this messy, inchoate, rock'n'roll masterpiece; the Rolling Stones in excelsis.

Stones in Exile will air on BBC2 on 23 May as part of the Imagine series