Artificial magnetic fields and spin-orbit couplings have been recently generated in ultracold gases in view of realizing topological states of matter and frustrated magnetism in a highly-controllable environment. Despite being dynamically tunable, such artificial gauge fields are genuinely classical and exhibit no back-action from the neutral particles. Here we go beyond this paradigm, and demonstrate how quantized dynamical gauge fields can be created in mixtures of ultracold atoms in optical lattices. Specifically, we propose a protocol by which atoms of one species carry a magnetic flux felt by another species, hence realizing an instance of flux-attachment. This is obtained by combining coherent lattice modulation techniques with strong Hubbard interactions. We demonstrate how this setting can be arranged so as to implement lattice models displaying a local Z2 gauge symmetry, both in one and two dimensions. We also provide a detailed analysis of a ladder toy model, which features a global Z2 symmetry, and reveal the phase transitions that occur both in the matter and gauge sectors. Mastering flux-attachment in optical lattices envisages a new route towards the realization of strongly-correlated systems with properties dictated by an interplay of dynamical matter and gauge fields.