You can do that via the ConnectAPI in Apex...it can be a bit complicated to code it, but thankfully, the nice folks on the team at SFDC built a helper class - you can see it here.
So to post a Happy Birthday message with an @mention to the user would look something like this if you have the above class installed:
String sText = 'Happy Birthday {005E0000005LZXh}!';
ConnectApi.FeedItem fi = ConnectApiHelper.postFeedItemWithMentions(null, 'me', sText);
The class parses for the {userId} syntax, and converts to be an @mention. I wrote a blog post about here which has some more context/examples