Mr. Mamdani, a state lawmaker and democratic socialist, will be the first Muslim mayor in the city’s history.