There have been at least two successful attempts to create GIF images that display their own MD5 hashes:
Hashquine by spq
Hashquine by Copyheart Rogdham
You can download both files and verify that the
md5sum hashes equal the hashes displayed in the images.
These rely on the fact that MD5 collisions are easy to produce nowadays, and the fact that the GIF format is a sequence of frames. Effectively, the GIFs consist of 32 chunks of animation data. Each chunk is calculated as a 16-way MD5 multicollision, i.e. there are 16 different chunks which produce the same hash but show different hexadecimal digits. So, the GIF is built by computing and concatenating all 32 16-way collisions, computing the resulting hash of the file, and then selecting the chunks which produce the desired output. In other words, the flexibility of the GIF format, and the weakness of MD5, allows the displayed hash to be chosen one digit at a time without affecting the hash of the image file.
In principle, a similar result could be achieved for any other hash function, so long as it is easy to produce collisions. For example, it would be easy to do this with the CRC family of hashes, since it is easy to collide those (just solve a linear equation). However, for hash functions that are currently collision-resistant, such as SHA-256, it is computationally infeasible.
Other file formats can be attacked in this way too: for example, issue 14 of the PoC||GTFO journal shows its own MD5 hash on the cover page of the PDF: https://www.alchemistowl.org/pocorgtfo/pocorgtfo14.pdf. PostScript, and even the NES ROM format can similarly be attacked thanks to file format tricks; read the journal for more technical details.