The nation’s first elected female leader is narrowly defeated in a major shift for Sweden.