Shepherds guide sheep in the streets of Madrid during the annual celebration of transhumance. Marcos del Mazo—Pacific Press/LightRocket/Getty Images

Shepherds guide sheep in the streets of Madrid during the annual celebration of transhumance.

The grass was greener on the other side for the thousands of sheep that walked through Madrid during Fiesta de la Transhumancia (Transhumance Festival), on Oct. 23, 2016.

The tradition started as an ancient agricultural practice of moving a flock of sheep from one field to another to prevent overgrazing. The migration happens twice a year as the animals move from higher altitudes during the summer and lower altitudes during the winter, the Digital Journal reported.

The area's shepherds struck a deal in 1418 that allowed the animals to walk through the city freely, however they had to pay a toll of 160 maravedies. A few shepherds still pay in the medieval currency in memory of the deal.

According to the National Post , the number of animals that take part in Transhumance has dropped from a million to a few thousand because of urban farming. The Ministry of Agriculture has been keeping the tradition alive by promoting the festival since 1994.