A Catalan separatist won municipal elections in Barcelona for the first time on Sunday, tailed closely by outgoing left-wing mayor Ada Colau.

France's former prime minister Manuel Valls lost his bet to become mayor of the Mediterranean seaside city in Catalonia, coming in fourth with just over 13 percent of the vote.

The winner was Ernest Maragall, a veteran politician from ERC, a party that wants Catalonia -- the northeastern region ruled by separatists -- to be independent from the rest of Spain.

He gained 10 seats out of 41 in the city council, as did Colau.

But Maragall beat her by fewer than 5,000 votes, and unless other groupings manage to build an alternative majority, he will become mayor of Spain's second largest city.

Catalonia was the scene of a failed attempt to secede from Spain in October 2017, when separatist leaders who ran the region went ahead with an independence referendum despite a court ban.

That was followed by a brief declaration of independence before Spain's then prime minister sacked the Catalan executive and called snap regional polls.

ERC leader Oriol Junqueras, who was Catalonia's vice-president at the time, is currently in jail as a trial over his and 11 others' role in the secession bid goes on.