This paper addresses the participation of an aggregator of small prosumers in the energy and tertiary reserve markets. A two-stage stochastic optimization model is proposed to exploit the load and generation flexibility of the prosumers. The aim is to define energy and tertiary reserve bids to minimize the net cost of the aggregator buying and selling energy in the day-ahead and real-time markets, as well as to maximize the revenue of selling tertiary reserve during the real-time stage. Scenario-based stochastic programming is used to deal with the uncertainties of photovoltaic power generation, electricity demand, outdoor temperature, end-users' behavior, and preferences. A case study of 1000 small prosumers from MIBEL is used to compare the proposed strategy to two other strategies. The numerical results show that the proposed strategy reduces the bidding net cost of the aggregator by 48% when compared to an inflexible strategy typically used by retailers.