Participants in socio-economic systems are often ranked based on their performance. Rankings conveniently reduce the complexity of such systems to ordered lists. Yet, it has been shown in many contexts that those who reach the top are not necessarily the most talented, as chance plays a role in shaping rankings. Nevertheless, the role played by chance in determining success, i.e. serendipity, is underestimated, and top performers are often imitated by others under the assumption that adopting their strategies will lead to equivalent results. We investigate the tradeoff between imitation and serendipity in an agent-based model. Agents in the model receive payoffs based on their actions and may switch to different actions by either imitating others or through random selection. When imitation prevails, most agents coordinate on a single action, leading to non-meritocratic outcomes, as a minority of them accumulate the majority of payoffs. Yet, such agents are not necessarily the most skilled ones. When serendipity dominates, instead, we observe more egalitarian outcomes. The two regimes are separated by a sharp transition, which we characterize analytically in a simplified setting. We discuss the implications of our findings in a variety of contexts, ranging from academic research to business.