Changing PWM on Arduino Mega, pins 9 & 10 to 20 to 25 KHz
Fortunately, the timer for PWM on both pins 9 and 10 on the Arduino Mega (but not necessarily other boards) is timer 2, so changing the PWM frequency of one will change the PWM frequency of the other.

You should also check that changing the the timer frequency does not effect any other libraries (eg Servo, from memory typically uses timer 2).

The frequency for the timer is set by the TCCR2B register (bits CS22, CS21 and CS20) see p 185 of the datasheet for the Mega2560 IC http://ww1.microchip.com/downloads/en/devicedoc/atmel-2549-8-bit-avr-microcontroller-atmega640-1280-1281-2560-2561_datasheet.pdf

As far as code goes, try the following (just tested with a small speaker, appears to change to pitch as expected, but I don't have a motor to try it on:

Each line sets the frequency for timer2 (PWM 9 and 10), should be ok to do during setup, and settings will remain. Note that the first line is equivalent to normal default values:

  TCCR2B=(TCCR2B&amp;248)|4;    //prescaler=64, f=977Hz

  TCCR2B=(TCCR2B&amp;248)|3;    //prescaler=32, 1953Hz

  TCCR2B=(TCCR2B&amp;248)|2;    //prescaler=8, 7812Hz

  TCCR2B=(TCCR2B&amp;248)|1;    //prescaler=1 62500Hz


Also check that your motor driver can handle the frequency.