MADRID — Spain’s governing Socialist party suffered an unexpected election setback on Sunday as right-wing parties won enough votes to oust the Socialists from power in the nation’s largest region for the first time since the country returned to democracy.

The result in Andalusia was a blow to Pedro Sánchez, Spain’s Socialist prime minister, who has been in office since June and could now be forced into a snap general election next year.

The Socialists won just 33 of the 109 seats in the regional parliament, according to provisional results released late Sunday, with 94 percent of the votes counted. It was the party’s worst result in Andalusia since 1982, when the region held its first election after Spain adopted a new Constitution.

The biggest change for Spain’s political landscape on Sunday was the emergence of Vox, a far-right party that had never previously made any significant election inroads. The party won 12 seats, according to the provisional results.