Sweden's conservative prime minister is resigning after a final vote count gave the opposition a three-seat majority in the country's election.