Partitioning tissues into compartments that do not intermix is essential for the correct morphogenesis of animal embryos and organs. Several hypotheses have been proposed to explain compartmental cell sorting, mainly differential adhesion, but also regulation of the cytoskeleton or of cell proliferation. Nevertheless, the molecular and cellular mechanisms that keep cells apart at boundaries remain unclear. Here we demonstrate, in early Drosophila melanogaster embryos, that actomyosin-based barriers stop cells from invading neighbouring compartments. Our analysis shows that cells can transiently invade neighbouring compartments, especially when they divide, but are then pushed back into their compartment of origin. Actomyosin cytoskeletal components are enriched at compartmental boundaries, forming cable-like structures when the epidermis is mitotically active. When MyoII (non-muscle myosin II) function is inhibited, including locally at the cable by chromophore-assisted laser inactivation (CALI), in live embryos, dividing cells are no longer pushed back, leading to compartmental cell mixing. We propose that local regulation of actomyosin contractibility, rather than differential adhesion, is the primary mechanism sorting cells at compartmental boundaries.