In this paper, the problem of distributed control of the heating, ventilation and air conditioning (HVAC) system in an energy-smart building is addressed. Using tools from game theory the interaction among several autonomous HVAC units is studied and simple learning dynamics based on trial-and-error learning are proposed to achieve equilibrium. In particular, it is shown that this algorithm reaches stochastically stable states that are equilibria and maximizers of the global welfare of the corresponding game. Simulation results demonstrate that dynamic distributed control for the HVAC system can significantly increase the energy efficiency of smart buildings.