It is estimated that this fantasia was composed by Mozart in 1782, mainly because of its similarities to his Prelude (Fantasy) and Fugure in C major, K. 394, also written in 1782, as well as another fantasy and a suite.
When Mozart moved to Vienna in 1781, he began to seriously study the works of Bach and other Baroque composers. This study would influence many of his later compositions.
Mozart left behind a lot of unfinished fugue fragments during this time period. These fugue fragments, as well as Mozart’s immersive study of Baroque music, is why it’s generally thought that this Fantasia was written during this time period.
Here’s is the music as beautifully performed by Emil Gilels: