Praise be to Allah.
The guideline on that ignorance which excuses a person and means that he is not breaking the fast if he commits an action that is deemed to break the fast is ignorance of the prohibition and ignorance of the fact that this thing breaks the fast.
If a person is unaware of the prohibition on masturbation and is unaware of the fact that masturbation breaks the fast, then his fast is not invalidated if he masturbates during the day in Ramadan.
But if a person is aware of the prohibition on masturbation, but he is unaware that masturbation breaks the fast, then he has no excuse, because he is required to avoid masturbation as soon as he knows that it is prohibited. If he does not refrain from it, this is indicative of heedlessness and transgression on his part, so his fast is invalidated thereby.
Shaykh Abu Bakr ad-Dimyaati (may Allah have mercy on him) said: “And not knowing also that it invalidates the fast” i.e., he is unaware of the prohibition and is unaware of the fact that it breaks the fast.
In other words, he is not excused for his ignorance unless he is unaware of both points, and this is the case. If he was not ignorant of both, such as if he knows the ruling on one of them, but not the other, then he is not to be excused, because it was incumbent upon him, once he knew that it is haraam, even if he did not know that it invalidates the fast – or vice versa – to refrain from that deed.
I‘aanat at-Taalibeen (2/260)
Shaykh Ibn ‘Uthaymeen (may Allah have mercy on him) said: We must understand the difference between ignorance of the ruling and ignorance of what results from that ruling:
Ignorance of the ruling means that the person is excused and there are no consequences to his action.
Ignorance of what results from the ruling means that what is obligatory is not waived. For example, if a man has intercourse during the day in Ramadan, knowing that it is haraam, but not knowing that the severe expiation is required for it, then the expiation is not waived in his case, and he must offer expiation. But if he thought that there was nothing wrong with it, i.e., that there was no prohibition on this action, then he is not obliged to do anything.
End quote from Fataawa Noor ‘ala ad-Darb by Ibn ‘Uthaymeen (11/2)
And Allah knows best.