Collective behaviour enhances environmental sensing and decision-making in groups of animals. Experimental and theoretical investigations of schooling fish, flocking birds and human crowds have demonstrated that simple interactions between individuals can explain emergent group dynamics. These findings indicate the existence of neural circuits that support distributed behaviours, but the molecular and cellular identities of relevant sensory pathways are unknown. Here we show that Drosophila melanogaster exhibits collective responses to an aversive odour: individual flies weakly avoid the stimulus, but groups show enhanced escape reactions. Using high-resolution behavioural tracking, computational simulations, genetic perturbations, neural silencing and optogenetic activation we demonstrate that this collective odour avoidance arises from cascades of appendage touch interactions between pairs of flies. Inter-fly touch sensing and collective behaviour require the activity of distal leg mechanosensory sensilla neurons and the mechanosensory channel NOMPC. Remarkably, through these inter-fly encounters, wild-type flies can elicit avoidance behaviour in mutant animals that cannot sense the odour--a basic form of communication. Our data highlight the unexpected importance of social context in the sensory responses of a solitary species and open the door to a neural-circuit-level understanding of collective behaviour in animal groups.