Arduino analogWrite source code explanation The source code for the analogWrite function looks like this:

void analogWrite(uint8_t pin, int val) {
    ...
    sbi(TCCRnA, COM0x);
    OCRnx = val;
    ...
}


Where n is which timer (0, 1, 2...) and x is which pin is controlled by that timer (A, B). 

This doesn't make sense to me, because the ATmega328P datasheet says that the bits TCCRnA.WGM0[1:0], and TCCRnB.WGM0 need to be set up as well.

Datasheet

Obviously the above code works, but how, with the missing bit settings?
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.