Context. Transverse loop oscillations and loop contractions are commonly associated with solar flares, but the two types of motion have traditionally been regarded as separate phenomena. Aims. We present an observation of coronal loops that contract and oscillate following the onset of a flare. We aim to explain why both behaviours are seen together and why only some of the loops oscillate. Methods. A time sequence of SDO/AIA 171 Å images is analysed to identify the positions of coronal loops following the onset of the M6.4 flare SOL2012-03-09T03:53. We focus on five loops in particular, all of which contract during the flare, with three of them oscillating as well. A simple model is then developed for the contraction and oscillation of a coronal loop. Results. We propose that coronal loop contractions and oscillations can occur in a single response to removal of magnetic energy from the corona. Our model reproduces the various types of loop motion observed and explains why the highest loops oscillate during their contraction, while no oscillation is detected for the shortest contracting loops. The proposed framework suggests that loop motions can be used as a diagnostic for the removal of coronal magnetic energy by flares, while rapid decrease in coronal magnetic energy is a newly identified excitation mechanism for transverse loop oscillations.