Arduino analogWrite source code explanation
Look at the init() function in wiring.c.  All of the timers get set up there depending on which chip is selected.  I don't know if some might also be done by initVariant().  But all it leaves for analogWrite to do is set the bit in the COM register to enable the PWM.