This question is addressed in Einei Shmuel, a commentary on Aggadic passages:
וכתבו התו' דהיינו דווקא במידי דאכילה ואמר מורי ז"ל הטעם דמה שמשמרין מאכילת איסור דווקא דהלא באמת שוגג אין כאן איסור כיון דלא ידע מהאיסור ול"ש בשוגג תקלה כיון דאינו עושה איסור כלל אמנם אם יאכל אדם סם המות אף דהאכילה היה בשוגג בוודאי יזיק לו ויימות ממנו וכ"כ באכילת איסור שמטמטם הלב והנפש והוא כסם המות להנפש לזאת אפי' בשוגג אף דלא יהיה עליו עונש מהאיסור אבל הנפש יזיק כסם המות ולכך מציל אותו השי"ת מזה עכד"ק
And Tosafot wrote that this is specifically by something of food. And my teacher of blessed memory said that the reason that they protect from forbidden foods specifically is that in truth there is no prohibition here since it was unintentional and he didn't know of the prohibition, and "ruin" is not applicable to an unintentional violation, since no prohibition was done at all. However, if a person eats poison, even if the eating was unintentional he will certainly get injured and die from it. The same is true by forbidden eating, for it sullies the heart and soul and is like poison for the soul. Therefore, even if done unintentionally and even though there is no punishment from the prohibition, but the soul is damaged like by poison. And therefore God saves him from this. End quote.