Due to the aging power-grid infrastructure and increased usage of renewable energies, microgrids (μGrids) have emerged as a promising paradigm. It is reasonable to expect that they will become one of the fundamental building blocks of a smart grid, since effective energy transfer and coordination of μGrids could help maintain the stability and reliability of the regional large-scale power-grid. From the control perspective, one of the key objectives of μGrids is load management using local generation and storage for optimized performance. Accomplishing this task can be challenging, however, particularly in situations where local generation is unpredictable both in quality and in availability. This paper proposes to address that problem by developing a new optimal energy management scheme, which meets the requirements of supply and demand. The method that will be described in the following models μGrids as a stochastic hybrid dynamic system. Jump linear theory is used to maximize storage and renewable energy usage, and Markov chain theory is applied to model the intermittent generation of renewable energy based on real data. Although the model itself is quite general, we will focus exclusively on solar energy, and will define the performance measure accordingly. We will demonstrate that the optimal solution in this case is a state feedback law with a piecewise constant gain. Simulation results are provided to illustrate the effectiveness of such an approach.