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.