This paper deals with an optimal operation of a microgrid in the electricity market and presents the communication between the distribution market operator and microgrid operator. The distribution market operator controls and manages the electricity market established in the distribution level, determining the amount of both electricity price and power exchange between market participants. The microgrid operator is able to purchase active and reactive power from the local distribution market. An effective short-term scheduling of the microgrid is implemented to ensure optimal operation. A risk-based stochastic model is used to model the prevailing uncertainties such as loads, wind generation, and main-grid availability in a market-based operation framework. Moreover, in this model, a linearized AC power flow is added to the mathematical formulations to offer a comprehensive solution to the security-constraint operation of the microgrid. The stochastic operation strategy is formulated as a mixed integer linear programming problem. Regarding the uncertainty modeling, the substation equipment failure is modeled with Monte-Carlo algorithm. The effectiveness of the risk-based stochastic method is demonstrated using a microgrid test bed in the presence of demand response resources, dispatchable and wind generation units as well as energy storage system. The results demonstrate demand response program can significantly reduce the operation cost in worst scenarios. Also, it is indicated that the risk-averse decisions reduce the risk of experiencing costly scenario. The proposed framework incorporating the distribution market constraints reduces the uncertainty in real-time operation as it can specified the required energy before running the problem. The deviations from assigned energy to the microgrid are penalized through the distribution market operator.