In this paper, a novel dynamic programming technique is presented for optimal operation of a typical renewable microgrid including battery energy storage. The main idea is to use the scenarios analysis technique to proceed the uncertainties related to the available output power of wind and photovoltaic units and dynamic programming technique to obtain the optimal control strategy for a renewable microgrid system in a finite time period. First, to properly model the system, a mathematical model including power losses of the renewable microgrid is established, where the uncertainties due to the fluctuating generation from renewable energy sources are considered. Next, considering the dynamic power constraints of the battery, a new performance index function is established, where the Lagrange multipliers and interior point method will be presented for the equality and inequality operation constraints. Then, a feedback control scheme based on the dynamic programming is proposed to solve the model and obtain the optimal solution. Finally, simulation and comparison results are given to illustrate the performance of the presented method.