Another reason Huck accepts the identity of Tom Sawyer is that it gives him a place to stay safe. Huck is still scared that there may be people after him, and staying with the Phelps' as Tom gives him a place to stay for a while, without having to be on the run, and it also gives him a chance to be close to Jim and help him escape.
Huck also idealizes Tom and Tom's adventurous plans. It may be somewhat of a confidence booster for Huck to pretend to be Tom for a bit. It also give Tom a rare chance to be part of a "real" family for a bit--to experience the love and care of family members for a small period of time.
When Huck goes to free Jim at the Phelps Plantation, he doesn't realize that the Phelps are the aunt and uncle of Tom Sawyer. They are also expecting a visit from Tom. So, when Huck shows up, Sally Phelps assumes Huck is Tom and Huck goes along with the ruse. Huck then picks up Tom Sawyer and Tom agrees to take on the identity of his younger brother, Sid.What is important to look for his the different ways Tom and Huck have of trying to help Jim escape. Tom, still filled with his romantic visions, wants to make Jim's escape like what in "all the books he's read." This proves to be both silly and dangerous at the same time.
Huck Finn has to pretend to be Tom Sawyer because he realizes that the family that is holding Jim is none other than the family of Tom Sawyer. Because he wants to maintain easy access to Jim so he can free him, he takes on Tom's identity so that he'll be welcomed like family while devising his plan.
As with most lies, this one that Huck creates spurs several other lies, and Tom, as a result, has to lie about his identity also.
The boys keep their secret regarding who is who until the end of the book where they manage to free Jim from the shack. While running away, Tom (the real Tom) is shot and nearly killed. Finally, in the end, the boys tell Aunt Sally and Uncle Silas who is who and why they did it.