We consider the problem of optimal reactive power compensation for the minimization of power distribution losses in a smart microgrid. We first propose an approximate model for the power distribution network, which allows us to cast the problem into the class of convex quadratic, linearly constrained, optimization problems. We also show how agents have a partial knowledge of the problem parameters and state via some local measurements. Then, we design a randomized, gossip-like optimization algorithm, providing conditions for convergence together with an analytical characterization of the convergence speed. The analysis shows that the best performance is achieved when we command cooperation among agents that are neighbors in the smart microgrid topology.