Pedro Sánchez, the prime minister of socialist Spain, accepted his defeat after right-wing parties won an absolute majority of seats in parliament.