This paper proposes a coordinated two-stage real-time market mechanism in an unbalanced distribution system which can utilize flexibility service from home energy management system (HEMS) to alleviate line congestion, voltage violation, and substation-level power imbalance. At the grid level, the distribution system operator (DSO) computes the distribution locational marginal prices (DLMP) and its energy, loss, congestion, and voltage violation components through comprehensive sensitivity analyses. By using the DLMP components in a firststage optimization problem, the DSO generates two price signals and sends them to HEMS to seek flexibility service. In response to the request of DSO, each home-level HEMS computes a flexibility range by incorporating the prices of DSO in its own optimization problem. Due to future uncertainties, the HEMS optimization problem is modeled as an adaptive dynamic programming (ADP) to minimize the total expected cost and discomfort of the household over a forward-looking horizon. The flexibility range of each HEMS is then used by the DSO in a second-stage optimization problem to determine new optimal dispatch points which ensure the efficient, reliable, and congestionfree operation of the distribution system. Lastly, the second-stage dispatch points are used by each HEMS to constrain its maximum consumption level in a final ADP to assign consumption level of major appliances such as energy storage, heating, ventilation and airconditioning , and water heater. The proposed method is validated on an IEEE 69-bus system with a large number of regular and HEMS-equipped homes in each phase.