Wilde entered prison on 25 March 1895, sentenced to two years' hard labor - a punishment that was considered more severe than mere penal servitude. He was first sent, briefly, to Newgate Prison for initial processing, the next week moved to Petonville prison, where "hard labor" consisted of many hours of pointless effort in walking a treadmill or picking oakum (separating the fibers in scraps of old navy ropes), and allowed to read only the Bible and Pilgrim's Progress. Prisoners were not allowed to speak to each other, and, out of their solitary cells, were required to wear a cap with a sort of thick veil so they would not be recognized by other prisoners. A few months later he was moved to Wandsworth Prison, which had a similar regimen. While he was there, he was required to declare bankruptcy, by which he lost virtually all his possessions including his books and manuscripts. On November 23, 1895 he was again moved, to the prison at Reading, which also had similar rules, where he spent the remainder of his sentence, and was assigned the third cell on the third floor of C ward - and thereafter addressed and identified only as "C.3.3" and not by name. Prisoners were identified only by their cell numbers and not by name. About five months after Wilde arrived at Reading Gaol, Charles Thomas Wooldridge, a trooper in the Royal Horse Guards, was brought to Reading to await his trial for murdering his common-law wife (and promptly presenting himself and confessing to a policeman) on March 29, 1896; on June 17th, Wooldridge was sentenced to death and returned to Reading for his execution, which took place on Tuesday, July 7th, 1896 - the first hanging at Reading in 18 years. Wilde was released from prison on May 18th, 1897 and he promptly went to France, never returning to Britain. He died in Paris, at the age of 46, on 30 November 1900.
While in France, surviving on an allowance from his wife of a mere three pounds a week - deliberately meagre to discourage the sort of high living that had led to his downfall - Wilde endeavored to find additional money. In August 1897, he sent the publisher Leonard Smithers an initial draft of the Ballad, which made such an impression that Smithers was enthusiatic about publishing it and even approached Aubrey Beardsley to do an illustration for it (which was not done). Thereafter there was a very active correspondence between the two of them, as Wilde was repeatedly revising and expanding the text, and supervising from afar the choice and size of typeface and the layout of the work. However, even the printing house hired to do the book demanded a change - for fear that the prison doctor would sue over the line which originally read "While the coarse-mouthed doctor gloats," - and this was changed to "While some coarse-mouthed doctor gloats." As one biographer (Ingleby) said, "Never, perhaps, since Gray's "Elegy" had a poem been so revised, pruned, and polished over and over again as this cry from a prison cell." Originally the first edition - with no assurance of a second edition - was planned for only 400 copies, but when Wilde calculated the printing expenses, he realized that even selling all 400 would not cover costs, and at his instigation Smithers instructed the printing house to double the number of copies and keep the printing plates in hopes of a reprinting. As publication day approached, Wilde was occasionally seized by a sort of panic over his finances and the risks of the poem failing to sell well, and made some half-hearted efforts to sell the poem's copyright for immediate cash; there were only a few disappointing nibbles and no such sale was made. Fortunately, the poem sold very well and very quickly, and obtained such a stir that subsequent printings also sold well for more than a year, assuring Wilde of a steady income which he did not outlive, as he died less than four years after the Ballad first appeared.
It has been suggested  that Wilde was, to some degree, inspired by poem IX in A.E. Housman's A Shropshire Lad (1896), which alludes to the hanging of condemned prisoners:
They hang us now in Shrewsbury jail: The whistles blow forlorn, And trains all night groan on the rail To men that die at dawn.
Although there is no specific documentation to show Wilde's active revising after the appearance of the first edition, there were some slight changes made in the second edition, which was printed only two weeks after the first edition went on sale. For example, in the first edition a line read "And his step was light" and in the second edition it becomes "And his step seemed light". These tiny alterations persisted through the seventh edition, the last edition handled by Smithers, and thereafter to most of the reprints. Since Smithers had the printers retain the plates from the first edition, it seems plausible that these were deliberate revisions done at Wilde's request.
Wilde did acknowledge (evidently to several people, since numerous separate sources recalled this) a glaring error in the very first line of the poem, "He did not wear his scarlet coat" - because Wooldridge, as a member of the Royal Horse Guards, had a blue uniform - but justified this poetic license because the second line would make no sense if it said "For blood and wine are blue."