We investigate the effects of gas accretion on small clusters of young stars. The evolution of clusters containing 3–10 stars and between 0.1 and 90 per cent of their masses in the form of gas is followed using a three-dimensional SPH code with sink-particles to treat the accretion of gas on to the stars. The gas accretion by the stars is highly non-uniform in that a few of the stars accrete significantly more than the rest. The location of the star in the cluster potential and its possible membership in a binary or multiple system are the primary factors in determining the accretion rate of the star. This competitive accretion process results in the formation of a spectrum of stellar masses, even when the initial stellar masses are uniform. Small variations in the initial masses are overwhelmed by the accretion process, whereas larger variations can affect the accretion dynamics as they affect the overall cluster potential. The differential accretion results in the massive stars being formed in the centre of the cluster. Their location is not due to an evolutionary effect of mass segregation. Implications of this competitive accretion process for determining the stellar mass spectrum are discussed.