I see this as a two step process:
1. Filter BAM file and get only mapped reads (How To Filter Mapped Reads With Samtools )
2. Output said mapped reads in FASTQ. (http://broadinstitute.github.io/picard/command-line-overview.html#SamToFastq )
To do this as efficiently as possible, using BBTools:
reformat.sh in=reads.sam out=mapped.fq mappedonly
Also, BBMap has a lot of options designed for filtering, so it can output in fastq format and separate mapped from unmapped reads, preventing the creation of intermediate sam files. This approach also keeps pairs together, which is not very easy using samtools for filtering.
bbmap.sh ref=reference.fa in=reads.fq outm=mapped.fq outu=unmapped.fq
You can first filter your raw bam file for mapped reads only: samtools view -F 4 in.bam -o out.bam. The -F options discards everything with the given flag, and flag 4 refers to all unmapped reads (see: http://broadinstitute.github.io/picard/explain-flags.html) and then use bedtools bamtofastq (http://bedtools.readthedocs.io/en/latest/content/tools/bamtofastq.html) to get the fastq from the bam.