The Sagrada Familia basilica in Barcelona has worldwide fame as an architectural treasure, the dreamlike masterpiece of the Catalan architect Antoni Gaudí, which draws millions of visitors a year though it is still under construction, 136 years after work began.

What it has not had for more than a century, according to the city, is a valid building permit.

The Sagrada Familia basilica has agreed to pay city authorities 36 million euros, or about $41 million, over 10 years to settle the dispute over the legality of the work and help pay for transportation improvements around the basilica.

Using the permit as leverage, the administration of Barcelona’s far-left mayor, Ada Colau, took on the Sagrada Familia, part of a trend of civil authorities around the country challenging the legal and tax status of Roman Catholic Church properties. Ms. Colau hailed the agreement, announced on Thursday, as historic.

The Sagrada Familia’s board had denied any wrongdoing, saying that it had a building permit — one issued in 1885 by Sant Martí de Provençals, which was an independent town at the time. Barcelona officials contend that after Sant Martí was absorbed into the city several years later, the construction required a Barcelona permit; the board says that for more than a century, no one asked for any such thing.