We consider a microgrid that consists of N providers and B consumers. Each provider has a certain supply and each consumer has a certain demand. The efficiency of transmitting energy between providers and consumers is modeled using a bipartite graph G. Our goal is to maximize the amount of utilized energy using a distributed algorithm that each provider runs locally. We propose a non-cooperative energy allocation game, and adopt the best-response dynamics for this game as our distributed algorithm. We prove that the best-response dynamics converge in no more than N steps to one of at most N! pure Nash equilibria of our game. Despite the fact that some of these Nash equilibria are suboptimal, we are able to prove that our algorithm achieves near-optimal performance in "almost all" games. We do so by analyzing the best-response dynamics in a random game, where the network is generated using a random model for the graph G. We prove that the ratio between the utilized energy of our algorithm and that of the optimal solution converges to one in probability as B increases (and N is any function of B). Using numerical simulations, we demonstrate that our asymptotic analysis is valid even for B - 10 consumers.