In History Today, Alexander Lee discusses the situation in Florence leading up to the time when Niccolò Machiavelli wrote his (in)famous work:
In 1512, however, everything fell apart. After a series of military defeats, Soderini was forced from office. With the help of Pope Julius II, Giuliano di Lorenzo de’ Medici was installed as the de facto ruler of Florence. The Republic collapsed.
Immediately, Giuliano purged the government and instituted a city-wide witch-hunt. As a prominent republican, Machiavelli was summarily dismissed from his positions in late 1512, and in 1513, a warrant was issued for his arrest. Accused of plotting against the Medici, he was tortured using a cruel technique known as the ‘strappado’ — which left his shoulders dislocated, and his whole body in excruciating pain — before being released and exiled to his country estate.
It was at this point that Machiavelli penned The Prince. Broken, depressed, and penniless, he saw it as his best chance of getting into the Medici’s good books, and of recouping his losses. Dedicating the book first to Giuliano di Lorenzo de’ Medici — the very man who had destroyed his life — and, after Giuliano’s death, to his nephew, Lorenzo, Machiavelli set out to provide not just a guide to princely government, but a positive justification of all of the terrible things to which he had fallen victim. Much like a fallen Politburo members at a Soviet show trial, Machiavelli defended his persecution in the hope of securing favour. Only later did he feel safe enough to express his republican sympathies more openly.