My understanding of this: There are three cases when past perfect is used 1) when it happened before certain event in past and is combined with action in simple past, or 2) in reported speech, or 3) in conditional sentences.
First use case like "I was very tired as I hadn't slept well for several days" often implies the event, in fact we distinguish between two actions that happened in past and were linked in particular order. In your case, without any other context the very rather action of closing the door must be in past perfect, because it happened before Mary's arrival, in "deeper" past. Otherwise ther might be some phrase before this one, that makes Mary's arrival a PERFECT action, for example someone met her in the hall after she had came.
In some cases it possible not to use past perfect tense in combination with simple past, that is if order of actions is natural and obvious, but is recommended to use one where clarity might be compromised