When comparing the reactivity of aluminum or magnesium to the reactivity of silver, we will find that both aluminum and magnesium are more reactive than silver.
Therefore, magnesium or aluminum can both replace the silver in the silver sulphide.
That's why a piece of aluminum or magnesium can be used to remove tarnish, which is mostly silver sulfide, on silverware.