(abridged) We develop a model which describes the coevolution of the mass function of dense cores and of the IMF in a protocluster clump. In the model, cores injected in the clump evolve under the effect of gas accretion. Accretion onto the cores follows a time-dependent accretion rate that describes accretion in a turbulent medium. Once the accretion timescales of cores exceed their contraction timescales, they are turned into stars. We include the effect of feedback by the newly formed massive stars through their stellar winds. A fraction of the winds energy is assumed to counter gravity and disperse the gas from the protocluster and as a consequence, quench further star formation. The latter effect sets the final IMF of the cluster. We apply our model to a clump that is expected to resemble the progenitor clump of the Orion Nebula Cluster (ONC). Our model is able to reproduce both the shape and normalization of the ONCs IMF and the mass function of dense cores in Orion. The complex features of the ONCs IMF,i.e., a shallow slope in the mass range ~0.3-2.5 Msol,a steeper slope in the mass range ~2.5-12 Msol, and a nearly flat tail at the high mass end are reproduced. The model predicts a rapid star formation process with an age spread for the stars of 2.3 10^5 yr which is consistent with the fact that 80% of the ONCs stars have ages of <=0.3 Myr. The model predicts a primordial mass segregation with the most massive stars being born in the region between 2-4 times the core radius of the cluster. In parallel, the model also reproduces, simultaneously, the mass function of dense cores in Orion. We study the effects of varying the model parameters on the resulting IMF and show that the IMF of stellar clusters is expected to show significant variations, provided variations in the clumps and cores properties exist.