Microgrids are subsystems of the distribution grid which comprises generation capacities, storage devices and flexible loads, operating as a single controllable system either connected or isolated from the utility grid. In this work, microgrid management system is developed in a stochastic framework. It is seen as a constraint-based system that employs forecasts and stochastic techniques to manage microgrid operations. Uncertainties due to fluctuating demand and generation from renewable energy sources are taken into account and a two-stage stochastic programming approach is applied to efficiently optimize microgrid operations while satisfying a time-varying request and operation constraints. At the first stage, before the realizations of the random variables are known, a decision on the microgrid operations has to be made. At the second stage, after random variables outcomes become known, correction actions must be taken, which have a cost. The proposed approach aims at minimizing the expected cost of correction actions. Mathematically, the stochastic optimization problem is stated as a mixed-integer linear programming problem, which is solved in an efficient way by using commercial solvers. The stochastic problem is incorporated in a model predictive control scheme to further compensate the uncertainty through the feedback mechanism. A case study of a microgrid is employed to assess the performance of the on-line optimization-based control strategy and the simulation results are discussed. The method is applied to an experimental microgrid: experimental results show the feasibility and the effectiveness of the proposed approach.