How can I use a media field in a custom form?
I found the answer to create the media autocomplete field like below.

// $id is your media ID.
$media = \Drupal\media_entity\Entity\Media::load($id); 

$form['upload_doc'] = [
  '#type'          =&gt; 'entity_autocomplete',
  '#target_type'   =&gt; 'media',
  '#default_value' =&gt; $media,
];