You should join the users table twice.
And I would go for LEFT JOIN
rather than INNER JOIN
as it is more appropriate.
SELECT messages.id, u1.username as name_from, u2.username as name_to, messages.contentFROM messages LEFT JOIN users u1 ON messages.id_from = u1.id LEFT JOIN users u2 ON messages.id_to = u2.id
The key here is that you join the tables with forign keys from messages table and their corresponding primary key from users table