This can be performed by the bib2bib tool, which provides pretty flexible and reliable ways to filter/extract/expand bibtex entries. This (little known) utility is part of the bibtex2html tool suite. (Note: you have to look for the PDF documentation, the HTML documentation does not discuss bib2bib!)
For instance, to flatten a bib file, one just writes:
bib2bib --expand --expand-xrefs -ob flattened.bib original.bib
It is also possible to specify filter and sorting options.
Multiple conditions can be grouped and combined with and/or/not to extract only entries matching certain criteria.
Edit: One thing that apparently is not supported is filtering out @comment entries. These entries are, for instance, inserted by JabRef. To get rid of them just pipe the result through some regex tool. Jukka suggested perl -p0 -e 's/\s*(\@comment\{\{[^}]*\}\}\s*)+/\n\n/g' for the job.