Compartments are units of cell lineage that subdivide territories with different developmental potential. In Drosophila, the wing and haltere discs are subdivided into anterior and posterior (A/P) compartments, which require the activity of Hedgehog, and into dorsal and ventral (D/V) compartments, needing Notch signaling. There is enrichment in actomyosin proteins at the compartment boundaries, suggesting a role for these proteins in their maintenance. Compartments also develop in the mouse hindbrain rhombomeres, which are characterized by the expression of different Hox genes, a group of genes specifying different structures along their main axis of bilaterians. We show here that the Drosophila Hox gene Ultrabithorax can maintain the A/P and D/V compartment boundaries when Hedgehog or Notch signaling is compromised, and that the interaction of cells with and without Ultrabithorax expression induces high levels of non-muscle myosin II. In the absence of Ultrabithorax there is occasional mixing of cells from different segments. We also show a similar role in cell segregation for the Abdominal-B Hox gene. Our results suggest that the juxtaposition of cells with different Hox gene expression leads to their sorting out, probably through the accumulation of non-muscle myosin II at the boundary of the different cell territories. The increase in myosin expression seems to be a general mechanism used by Hox genes or signaling pathways to maintain the segregation of different groups of cells.