You could
SELECT a.id, a.topic_id
FROM MSG a
WHERE a.id IN (
SELECT t.id
FROM MSG t
WHERE a.topic_id = t.topic_id
ORDER BY t.id DESC
LIMIT 2 )
ORDER BY a.topic_id, a.id
EDIT:
As it seems that mysql does not allow (yet! it'll be possible in future releases) to use LIMIT in subqueries here's a generalized solution (with no short-cut assumptions, except that msg.id is unique per topic_id):
SELECT a.id, a.topic_id
FROM MSG a
WHERE a.id IN (
SELECT MAX(t.id)
FROM MSG t
WHERE a.topic_id = t.topic_id
) OR
a.id IN (
SELECT MAX(t.id)
FROM MSG t
WHERE a.topic_id = t.topic_id AND
t.id NOT IN (
SELECT MAX(t2.id)
FROM MSG t2
WHERE t.topic_id = t2.topic_id
)
)
ORDER BY a.topic_id, a.id
of course this is not nice, but there you are. If assumption that ids in topic_id are ascending with no holes can be made, further improvements to the query can be made.