Faculty at prestigious institutions produce more scientific papers, receive more citations and scholarly awards, and are typically trained at more-prestigious institutions than faculty with less prestigious appointments. This imbalance is often attributed to a meritocratic system that sorts individuals into more-prestigious positions according to their reputation, past achievements, and potential for future scholarly impact. Here, we investigate the determinants of scholarly productivity and measure their dependence on past training and current work environments. To distinguish the effects of these environments, we apply a matched-pairs experimental design to career and productivity trajectories of 2,453 early-career faculty at all 205 PhD-granting computer science departments in the United States and Canada, who together account for over 200,000 publications and 7.4 million citations. Our results show that the prestige of faculty's current work environment, not their training environment, drives their future scientific productivity, while current and past locations drive prominence. Furthermore, the characteristics of a work environment are more predictive of faculty productivity and impact than mechanisms representing preferential selection or retention of more-productive scholars by more-prestigious departments. These results identify an environmental mechanism for cumulative advantage, in which an individual's past successes are "locked in" via placement into a more prestigious environment, which directly facilitates future success. The scientific productivity of early-career faculty is thus driven by where they work, rather than where they trained for their doctorate, indicating a limited role for doctoral prestige in predicting scientific contributions.