In the summer of 1516, Michelangelo worked out a third contract for the tomb of Julius and then returned to Florence, which was now ruled by the Duke of Urbino, who was in turn replaced by Giulio de' Medici. Michelangelo also took on a protégé, the painter Sebastiano del Piombo. The reigning Pope, Leo X, offered Michelangelo numerous commissions to improve Florence, all of which revolved around the considerable additions being made to San Lorenzo, the Medici family's parish church. Planning for the additions to San Lorenzo began in 1517 with models for a new facade for the church, which was Michelangelo's first serious architectural undertaking. Concurrently with the design for the San Lorenzo facade, Michelangelo worked on a Statue of Christ and continued working on the tomb of Julius in Rome and in Florence. He even began the monumental task of removing marble from the quarry for the San Lorenzo project, and was outraged when the contract was canceled in 1520. By 1520, Michelangelo was the unquestioned master of Italian art, since both of his primary rivals, Leonardo and Raphael, were both dead. He continued with his poetic efforts, and made his first mention of his homosexual longings. Pope Leo X suddenly died in 1521.
In 1521, while the Reformation gained momentum, Michelangelo began working on the Medici Chapel and on the Biblioteca Laurenziana projects. He also continued his series of unfinished Slaves, which were never used for the tomb of Julius, but which remain as important testaments to the sculptor's working process. The Chapel project was an architectural and sculptural fusion of Christian and Classical motifs that entailed the construction of tombs for two Medici dukes. As part of the Chapel, Michelangelo worked on the Allegories of the Phases of the Day, four nudes representing Dawn, Day, Evening, and Night, until 1534. Even then, the Chapel was incomplete, and the position of the nudes on the sarcophagi is most likely not their originally intended position. However, they are among Michelangelo's finest nudes and are excellent examples of his combining action with repose. Day and Evening are male figures, the former robust and young, the latter aging and limp; Dawn and Night, although female, are obviously based on male models, who represented for Michelangelo the superior human form. Meanwhile, in 1523 Leo X was succeeded by Giulio de'Medici, Leo's cousin and another of Michelangelo's childhood friends, who became Pope Clement VII. In 1525 Michelangelo signed another contract for the tomb of Julius, which had, at this point, been drastically scaled down.
Michelangelo worked simultaneously on the tomb, the Chapel, and the biblioteca until 1527, when Spanish and German mercenaries hired by Holy Roman Emperor Charles V invaded, sacked, and devastated Rome. Famine and plague followed the violence. The Florentines drove the Medicis out of Florence once again, and proclaimed a second Florentine Republic. In 1529, Charles V and Clement VII made a peace agreement that stipulated that the Medicis should return to power once more. The Florentines decided to protect themselves from the allied forces of the Pope and the Holy Roman Empire, and hired Michelangelo to design new fortifications for the city. This project occupied him for much of 1528 and 1529. In the midst of the military project, Michelangelo visited Ferrara to do some painting and ask military advice of the Duke there. He returned to Florence in September, but promptly fled to Ferrara and then to Venice to avoid the imminent invasion. Michelangelo returned, however, after the Florentines accused him of treason, and was in Florence when Charles V's troops took the city and restored power to Clement VII. Michelangelo, accused of treason by both the Florentines and the Roman Pope, went into hiding. When Clement the VII offered him immunity if he would work on the Medici Chapel, Michelangelo emerged and resumed work on San Lorenzo, overseeing its construction until August 1532.