A former prime minister was elected Somalia's president on Wednesday, declaring a new "era of unity" as he took on the daunting task of bringing the long-chaotic country its first fully functioning central government in a quarter-century.