We numerically investigate whether and how gaseous ejecta from AGB stars can be converted into new stars within originally massive star clusters (MSCs) in order to understand the origin of multiple stellar populations in globular clusters (GCs). We adopt a scenario in which (i) MSCs with masses of Ms can be formed from high-mass, high-density giant molecular clouds (GMCs) in their host galactic building blocks embedded in dark matter haloes at high redshifts, and (ii) their evolution therefore can be significantly influenced by Ms, their initial locations and physical properties of their hosts. Our 3D hydrodynamical simulations show that gaseous ejecta from AGB stars can be retained within MSCs and consequently converted into new stars very efficiently in the central regions of MSCs, only if Ms exceeds a threshold mass (Mth) of ≈106 M⊙. The new stars can correspond to the ‘second generation (SG)’ of stars with higher Na and lower O abundances observed in GCs. Star formation efficiencies during the formation of SG stars within MSCs with Ms≥Mth can be rather high (0.3–0.9) so that very compact new clusters within original MSCs can be formed. Ms should be as large as 106–107 M⊙ to explain the observed large fraction of SG stars in the present ordinary Galactic GCs, because new stars can consist of only 1–4 per cent among all stars for the standard initial mass function. Nuclear MSCs are found to retain much more effectively the AGB ejecta and convert more efficiently the gas into new stars, owing to the much deeper gravitational potential of their hosts. Capture and accretion of cold molecular gas (or small GMCs) by forming MSCs themselves can be mechanisms for mixing (i.e., dilution) of AGB ejecta with cold pristine gas. We suggest that both Ms and their locations within their hosts can determine whether abundance spread can be seen only in light elements or even in heavy ones. We discuss how and in what time-scale MSCs preferentially lose old stars owing to tidal stripping by their host galactic building blocks. We also suggest that the origin of the intermediate-age GCs with possible age spread of ∼100 Myr yet apparently no/little abundance spread in light elements in the LMC is closely associated with their incapability to retain the AGB ejecta owing to their low masses.