Legend has it that Miguel de Cervantes’ masterpiece Don Quixote was conceived and at least partially written in prison – “where every discomfort has its place and every mournful sound makes its home,” according to its opening lines..

Stripped of freedom and with his vision acutely attuned to the ironies of his circumstances, Cervantes broke through the literary conventions of his time. Can prison be a muse? It hardly seems desirable, when freedom is the condition most of us would choose. Yet history shows that lasting work can be inspired by the horrors and deprivations of incarceration. Authors with the intellectual grit to endure have been rewarded with exceptional insights into human behaviour and psychology. The tension between freedom and captivity has led to unexpected creative breakthroughs.

Don Quixote contains “practically every imaginative technique and device used by subsequent fiction writers to engage their readers and construct their works,” writes Edith Grossman in the preface to her 2003 translation. Cervantes anticipates realism, modernism, post-modernism, the frame story, the mixing of genres, and more, all while maintaining that ironic wit. His device of characters commenting upon the text in which they appear is centuries ahead of its time.

Cervantes’ masterpiece seems to have been inspired by the physical and psychological pressures of confinement, linking the first modern novel with the prison experience. And Don Quixote has endured, interpreted by critics from myriad angles, shaping the work of scores of writers in succeeding generations.

Fyodor Dostoyevsky credits Don Quixote as a precursor to his portrait of a positively good man, the naive epileptic Prince Myshkin in The Idiot. “Of the good figures in Christian literature, the most complete is that of Don Quixote,” he noted in 1868 while working on the novel. “But he is good only because at the same time he is ridiculous.” Prince Myshkin, whose goodness blinds him to the subtleties of deceit and betrayal, is unable to function in society. Like Cervantes, Dostoyevsky presents the state of goodness as verging on madness.

Dostoyevsky, too, was profoundly changed by his prison experience. He had already published his first novel, Poor Folk, when he was arrested in 1849 for involvement with a group of leftist St Petersburg intellectuals. After months in prison, he was sentenced to death, carted with others in his group to Semyonovsky Square and prepared for the firing squad. At the last minute the Tsar stayed his execution but Dostoyevsky spent four years of hard labour in the Siberian gulag, where his educated status inflamed other inmates. “They are a coarse, irritated, and embittered lot,” he wrote to his brother. “Their hatred for the gentry passes all limits.”

Dreams of freedom

Dostoyevsky’s prison experience ushered in an awareness of the irrational and of a sense of communal suffering. His best novels, including Crime and Punishment and The Brothers Karamazov, are gems of psychological insight. James Joyce wrote that Dostoyevsky “created modern prose, and intensified it to its present-day pitch.” Mikhail Bakhtin identified the ‘polyphonic’ qualities of Dostoyevsky’s work, which expanded the novel to include many conflicting voices rather than a single vision.

His 1861 novel From the House of the Dead, or Prison Life in Siberia, written as fiction from the point of view of a man who has killed his wife, documents his own prison experience. “Money is coined liberty, and so it is ten times dearer to a man who is deprived of freedom,” he writes. He explains the prison trade in vodka and tobacco, the compulsion to steal. His fictional inmate dreams of freedom relentlessly, as did its author.

This yearning for freedom while enduring the hardship of prison is a thread through Aleksandr Solzhenitsyn’s literary lifework, which began during his eight years in Soviet labour camps. He was arrested in 1945 for making disparaging remarks about Stalin in a letter. After finishing his sentence in 1955, he was exiled to southern Kazakhstan. In solitude, beset by harrowing memories, he composed his first novel, not expecting it to ever be published. One Day in the Life of Ivan Denisovich, set during one bitterly cold day in a Siberian labour camp in 1951, was published in 1962, nine years after Stalin’s death, to global acclaim. It was the first literary work to expose the degradations of the Soviet regime’s gulags.

Solzhenitsyn wrote a series of novels, including The Cancer Ward, in which he asked, “A man dies from a tumour, so how can a country survive with growths like labour camps and exiles?” His masterwork The Gulag Archipelago, completed in 1968, is a massive three-volume indictment of the regime’s forced labour camps. Subtitled An Experiment in Literary Investigation, it moves in excruciating detail through the process of interrogation, transportation, imprisonment and aftermath, including the massacre of inmates.

Solzhenitsyn drew on his own experience, hundreds of interviews and historic documents. He distilled them into a shattering narrative that reveals the inner workings of a murderous state within a state. His polyphonic form was noted in the citation when Solzhenitsyn won the 1970 Nobel Prize for Literature: “each person becomes the chief character whenever the action concerns him. This is not just a technique, it is a creed. The narrative focuses on the only human element in existence, the human individual, with equal status among equals, one destiny among millions and a million destinies in one. This is the whole of humanism in a nutshell, for the kernel is love of mankind.”

Clear vision

Examining the relationship of the individual to the state and the question of goodness was also a theme of the 19th-Century American political thinker Henry David Thoreau. Thoreau was deeply affected by the night he spent in prison for refusing to pay a poll tax. “I saw yet more distinctly the State in which I lived,” he wrote.

This episode inspired his 1848 speech Resistance to Civil Government, later published as Civil Disobedience. “A minority is powerless while it conforms to the majority,” he wrote, “… but it is irresistible when it clogs by its whole weight. If the alternative is to keep all just men in prison, or give up war and slavery, the State will not hesitate which to choose.” The insights Thoreau developed from witnessing first-hand the power of the state to jail citizens, had far-ranging consequences.

His thinking about the obligation of the individual to question the actions of the State influenced generations of future thinkers from Leo Tolstoy to Gandhi to Martin Luther King Jr. King’s 1963 Letter from a Birmingham Jail, in which he noted, “I am in Birmingham because injustice is here,” is a classic document in the civil-rights movement. King credited Thoreau with convincing him that non-cooperation with evil is as much a moral obligation as co-operation with good. "No other person has been more eloquent and passionate in getting this idea across than Henry David Thoreau,” King wrote. “As a result of his writings and personal witness, we are the heirs of a legacy of creative protest."

It could be argued that Cervantes, Dostoyevsky, Solzhenitsyn and Thoreau might not have written so brilliantly without being inspired by prison. Confinement is onerous, but there can be redeeming aspects. As these writers, and countless others have shown, prison, in tandem with the spacious human imagination and the dream of freedom, can inspire literary masterpieces.

If you would like to comment on this story or anything else you have seen on BBC Culture, head over to our Facebook page or message us on Twitter.