We study numerically the formation of molecular clouds in large-scale colliding flows including self-gravity. The models emphasize the competition between the effects of gravity on global and local scales in an isolated cloud. Global gravity builds up large-scale filaments, while local gravity -- triggered by a combination of strong thermal and dynamical instabilities -- causes cores to form. The dynamical instabilities give rise to a local focusing of the colliding flows, facilitating the rapid formation of massive protostellar cores of a few 100 M$_odot$. The forming clouds do not reach an equilibrium state, though the motions within the clouds appear comparable to ``virial. The self-similar core mass distributions derived from models with and without self-gravity indicate that the core mass distribution is set very early on during the cloud formation process, predominantly by a combination of thermal and dynamical instabilities rather than by self-gravity.