In this paper, we consider the balancing problem in a hierarchical market-based structure for smart energy grids that is based on the Universal Smart Energy Framework. The large-scale introduction of renewable, intermittent energy sources in the power system can create a mismatch between the forecasted (day-ahead) and the actual supply and demand. Without a proper control strategy, this deviation could lead to network overloads and commercial losses. We present a multilevel distributed optimal control formulation to the problem, in which the appliances of prosumers that can provide flexibility are optimally dispatched based on local information. The control strategy takes the capacity limitations of the distribution network into account. We provide example simulation results, obtained by distributed model predictive control. Note to Practitioners-We propose a control strategy that aims to minimize the imbalance between forecasted and actual supply and demand in electricity grids. This is important, because the imbalance can lead to commercial losses for the stakeholders. Since the number of agents (i.e., households) in the power network is typically large, centralized controllers are not feasible due to scalability issues. We instead develop a distributed controller that solves the problem using only local information. We demonstrate our algorithm through simulations, which are implemented on a single computer. In practice, households can have smart meters on which the individual controllers run, thereby obtaining the solution in a parallel fashion.