Friedrich Merz has been elected chancellor by parliament in a second round of voting after an initial defeat.