The relationship between biodiversity and ecosystem function (BEF) captivates ecologists, but the factors responsible for the direction of this relationship remain unclear. While higher ecosystem functioning at higher biodiversity levels (‘positive BEF’) is not universal in nature, negative BEF relationships seem puzzlingly rare. Here, we develop a dynamical consumer-resource model inspired by microbial decomposer communities in pitcher plant leaves to investigate BEF. We manipulate microbial diversity via controlled colonization and measure their function as total ammonia production. We test how niche partitioning among bacteria and other ecological processes influence BEF in the leaves. We find that a negative BEF can emerge from reciprocal interspecific inhibition in ammonia production causing a negative complementarity effect, or from competitive hierarchies causing a negative selection effect. Absent these factors, a positive BEF was the typical outcome. Our findings provide a potential explanation for the rarity of negative BEF in empirical data.