A luminous X-ray source is associated with MGG 11-a cluster of young stars ~200pc from the centre of the starburst galaxy M 82 (refs 1, 2). The properties of this source are best explained by invoking a black hole with a mass of at least 350 solar masses (350Msolar), which is intermediate between stellar-mass and supermassive black holes. A nearby but somewhat more massive cluster (MGG 9) shows no evidence of such an intermediate-mass black hole, raising the issue of just what physical characteristics of the clusters can account for this difference. Here we report numerical simulations of the evolution and motion of stars within the clusters, where stars are allowed to merge with each other. We find that for MGG 11 dynamical friction leads to the massive stars sinking rapidly to the centre of the cluster, where they participate in a runaway collision. This produces a star of 800-3,000 Msolar, which ultimately collapses to a black hole of intermediate mass. No such runaway occurs in the cluster MGG 9, because the larger cluster radius leads to a mass segregation timescale a factor of five longer than for MGG 11.