It works this way because Mastodon and Lemmy both implement ActivityPub.
When you mention a Lemmy community from a Mastodon post, this post is sent to the Lemmy server you mentioned and treated as a new post in the community by Lemmy.
Then, when people reply from their Lemmy client, these comments are also visible to on the OP’s Mastodon server.
It works this way because Mastodon and Lemmy both implement ActivityPub.
When you mention a Lemmy community from a Mastodon post, this post is sent to the Lemmy server you mentioned and treated as a new post in the community by Lemmy.
Then, when people reply from their Lemmy client, these comments are also visible to on the OP’s Mastodon server.