We studied experimentally and numerically the compaction and subsequent expansion dynamics of a granular bed composed of cylindrical repelling magnets contained in a two-dimensional cell. The particles are firstly compressed vertically with a piston at a given strain rate until a maximum force is reached. The piston is then removed at the same strain rate while the bed expands due to the magnetic repulsion of the particles. In the experiments, two different initial configurations were generated, a standard and a loose packing bed. The standard packing bed was simulated, and modelling the dry friction between the magnetic particles and the walls of the cell was crucial for the correct description of the compression and expansion dynamics. We found that the force acting on the piston increases continuously and exponentially with the piston stroke during compression, being very sensitive to the initial packing conditions of the bed. In contrast, a history-independent exponential decrease of this force was found during the expansion phase. The hysteresis in the system was quantified in terms of the average displacement of the particles. The continuous compression contrasts with the sudden force drops observed during the compaction of granular materials with direct particle-particle contacts, where stick-slip motion is induced by friction and force chain breakage. Moreover, we found that the short range of magnetic interaction induces density inversion and crystallization of the system. Our results can be useful to develop a new kind of magnetic granular dampers.