Thanks for sending your Unity reproduction project up front. Unfortunately Unity seems to not properly respect the
Sorting Layer  during raycasts between
SpriteRenderer objects. It also seems to matter whether the
BoxCollider2D is attached to the
Renderer GameObject or to its parent, the
Sorting Layer of the renderer only seems to be respected properly when the
BoxCollider2D is at the renderer as well (otherwise it may count as the default sorting layer perhaps?). So we're afraid to say that we can't do anything about it, as it's in Unity's hand to handle the raycast. If you really need to use
Sorting Layer, we would recommend to either create dummy raycast receivers (e.g. empty
SpriteRenderers with a
BoxCollider2D) that forward any events to the intended receiver, or to change raycast handling altogether.
 In code it can be set via
renderer.sortingLayerID = SortingLayer.NameToID(layerName);.