Starting from a higher Courant bracket associated to exceptional generalized geometry, we provide a systematic derivation of all types of fluxes and their Bianchi identities for four-dimensional compactifications of M-theory. We show that these fluxes may be understood as generalized Wess-Zumino terms in certain topological threebrane sigma-models of AKSZ-type, which relates them to the higher structure of a Lie algebroid up to homotopy. This includes geometric compactifications of M-theory with G-flux and on twisted tori, and also its compactifications with non-geometric Q-and R-fluxes in specific representations of the U-duality group SL(5) in exceptional field theory.