The self-organized motion of vast numbers of creatures in a single direction is a spectacular example of emergent order. Here, we recreate this phenomenon using actuated nonliving components. We report here that millimetre-sized tapered rods, rendered motile by contact with an underlying vibrated surface and interacting through a medium of spherical beads, undergo a phase transition to a state of spontaneous alignment of velocities and orientations above a threshold bead area fraction. Guided by a detailed simulation model, we construct an analytical theory of this flocking transition, with two ingredients: a moving rod drags beads; neighbouring rods reorient in the resulting flow like a weathercock in the wind. Theory and experiment agree on the structure of our phase diagram in the plane of rod and bead concentrations and power-law spatial correlations near the phase boundary. Our discovery suggests possible new mechanisms for the collective transport of particulate or cellular matter.