The importance of mergers for the origin of intracluster stars in cosmological simulations of galaxy clusters