How does the brain know what to crave?
Check out this question on biology.stackexchange: Do omnivore mammals vary food preferences based on dietary needs?

The answers in that question mention that experiments on Rats and Birds determined that there's an internal chemosensor, the anterior piriform cortex (APC) within bird and rat brains that senses lack of Indispensible Amino Acids (IAA). Animals show aversion to foods that lack IAA in as short as 20-30 minutes. 

There's another example listed that mentions that being sodium deficient makes animals like salt more. Their preference for salty taste increases.

I don't know if these findings apply to humans though.