Spain's governing Socialists won the country's election but fell short of a majority as a far-right party won seats in parliament for the first time since the death of the dictator Francisco Franco.

With nearly all ballots counted, Prime Minister Pedro Sanchez's Socialists won nearly 29 per cent of the votes, capturing 122 seats in the 350-seat Congress of Deputies meaning they will need the backing of smaller parties to govern.

The new far-right Vox party won 10 per cent of the vote in Sunday's election and will have 24 seats. Its success came at the expense of the once-dominant conservative Popular Party, which fell to 66 seats

The Popular Party lost votes both to Vox and to the center-right Citizens party, which will increase its number of seats from 32 to 57.

Spain's incumbent Prime Minister and Spanish Socialist Workers' Party (PSOE) leader Pedro Sanchez greets supporters outside of the PSOE headquarters on Sunday night

Santiago Abascal giving a speech at the headquarters of Vox on Sunday night in Madrid

Sunday night's election saw the Socialists win the election but with no overall majority as the far-right Vox party won seats for the first time

The surge in votes for Vox shows that leader Santiago Abascal has managed to resurrect the far-right from the margins of Spanish politics where it has spent four decades.

Spain has had no far-right party to speak of since the death of dictator Francisco Franco in 1975.

The results raise the spectre of another period of instability for Spain, with Sanchez depending on alliances with hostile rivals in an environment that has soured since Catalonia's failed secession bid in 2017.

Sanchez's Socialist Party (PSOE) got 123 lawmakers out of 350, or close to 29 percent of votes - short of an absolute majority but much better than the 85 seats it got in 2016.

'The Socialists have won the general election and with it the future has won and the past has lost,' he told cheering supporters from the balcony of the party's headquarters in Madrid, claiming victory late Sunday.

The big loser was the conservative Popular Party (PP), which bagged 66 seats compared to 137 in the previous election that saw it govern Spain with a minority government.

Sanchez, who came to power in June after ousting conservative prime minister Mariano Rajoy in a no-confidence vote, could seek to forge alliances with far-left Podemos and smaller groupings like Catalan separatist parties, as he had done over the past 10 months.

He could also try to cosy up to centre-right Ciudadanos, which won 57 seats. Together, they would form an absolute majority but voters from both parties would likely frown on such a move.

'I hope Sanchez won't reach an agreement with Ciudadanos, I want a left-wing government,' 51-year-old Esther Lopez, said at the Socialist Party headquarters, wearing earrings marked 'PSOE.'

Ciudadanos leader Albert Rivera, built his campaign on disparaging Sanchez, criticising his attempts to negotiate with Catalan separatist parties in a bid to ease a secession crisis in the northeastern region.

Supporters of Republican Left party of Catalonia celebrate the results of the general election in Barcelona, Spain on Sunday night

Spain's Prime Minister and Socialist Party leader Pedro Sanchez gestures to supporters outside the party headquarters following the general election in Madrid on Sunday

Voters in Spain had become disillusioned as the country struggled with a recession, austerity cuts, corruption scandals, divisive demands for independence from the restive Catalonia region and a rise in far-right nationalism not seen since Spain's dictatorship ended in the 1970s.

Sanchez announced that he would soon open talks with other political parties, telling crowds gathered at the gates of his party headquarters in central Madrid that 'the future has won and the past has lost.'

He hinted at a preference for a left-wing governing alliance but also sent a warning to Catalan separatists whose support he may need that any post-electoral pact must respect the country's 1978 constitution, which bans regions from seceding.

'We told you that we were going to begin a reconquering of Spain and that's what we have done,' Vox leader Santiago Abascal said, in reference to the 15th-century campaign by the Spanish Catholic Kings to end Muslim rule in the Iberian Peninsula.

Vox, which was formed five years ago, has promised to defend Spain from its 'enemies,' citing feminists, liberal elites and Muslims among others. Its emergence on the national stage gives Spain five political parties, furthering political fragmentation in a country that was alternately ruled for decades by the Socialists and the Popular Party.

A divided Spain voted on Sunday in its third general election in four years. Pictured are supporters of far right party Vox cheer as party leader Santiago Abascal addresses them outside the party headquarters following the general election in Madrid

Santiago Abascal, leader of far-right party Vox, addresses supporters gathered outside the party headquarters following the general election in Madrid on Sunday

To stay in office, the Socialists and Sánchez must form a governing alliance with smaller parties, including the far-left United We Can led by Pablo Iglesias.

Iglesias said after the vote the he 'would have liked a better result, but it's been enough to stop the right-wing and build a left-wing coalition government,' adding that he's already offered support to Sanchez.

But Sánchez will still need 11 more seats to get the 176-seat majority he needs in the lower house of parliament, meaning he may be forced to make pacts with Catalan and other separatist parties - moves that would anger many Spaniards on the left and the right.

Pablo Casado, who had steered the Popular Party further to the right to try to stop it from losing votes to Vox, called the worst ballot result ever for his party 'very bad,' saying 'we've been losing our electoral support for several elections.'

Turnout was nearly 76 per cent, up more than 8 points since the previous election in 2016. The vote surge included a huge boost in the northeastern Catalonia region, which has been embroiled in a political quagmire since its failed secession bid in 2017 put separatist leaders in jail while they are tried.

Supporters of Spanish Prime Minister and Socialist Party candidate Pedro Sanchez gather at the party headquarters waiting for the results

The arrival of Vox in Madrid's national parliament marks a big shift for Spain, where the far right has not played a significant role since the country's transition to democracy following the death of dictator Gen. Francisco Franco in 1975.

The Popular Party and the Citizens party had focused their campaigns on unseating Sánchez, hinting they could create a conservative coalition government - with the backing of Vox - like a regional one that recently ousted the Socialists from southern Andalusia.

Speaking Sunday after voting, Sánchez said he wanted a mandate to undertake key social and political reforms.

The prime minister said he wanted 'a stable government that with calmness, serenity and resolution looks to the future and achieves the progress that our country needs in social justice, national harmony' and in fighting corruption.

At the Palacio Valdes school in Madrid, voter Alicia Sánchez, a 38-year-old administrator, worried about the influence of Vox.

'I've always come to vote, but this time it feels special. I'm worried about how Vox can influence policies on women and other issues. They are clearly homophobic. Reading their program is like something from 50 years ago,' she said.

Having voted in all elections since Spain returned to democratic rule four decades ago, Amelia Gómez, 86, and Antonio Román, 90, said they had little faith in any of the candidates.

'All I want is for whoever wins to take care of the old people,' Gómez said, complaining that together the two of them receive less than 1,000 euros ($1,100) a month in state pensions.