You have asked a very perceptive question, because while all critics agree that Huck as a character undoubtedly changes, develops and matures during the middle section of the book, a number of critics argue that the final section of the book (Chapter 31 to the end) represent a regression of Huck as he becomes, once again, a mere sidekick to Tom Sawyer's hair-brained schemes and all the interesting moral development we have observed in the middle section is lost.
However, Huck's experiences of society and, in particular, of being 'sivilised' have made him distrustful and suspicious. Therefore, as he travels down the river with Jim, this forces him to question the things society has taught him. The major example of this is Huck's relationship with Jim. According to the law, Jim is the property of Miss Watson, but, to Huck and his sense of logic and fairness, it just feels 'right' to help Jim. Thus in the middle section of the novel Huck's natural intelligence and his willingness to persevere and think through a situation to its logical conclusion leads him to some decisions that are correct in their natural context, but would shock the society of the time that this novel is set in. An excellent example of this would be when Huck and Jim encounter a group of slave-hunters, and Huck discovers the value of a lie - sometimes, because of the problems in society, lieing is the right course of action.
Interestingly, because Huck is a child, he looks upon the world with child-like eyes. All the experiences he encounters provoke deep thought as he tries to work out what is 'right' and what is 'wrong' and what is just ridiculous. Huck's willingness to create his own rules in situations is what makes this novel so fascinating as he confronts society in all its guises and so often finds it wanting. Yet, as many critics have established, Huck is still part of and a product of his society, and thus we see him struggling with initial preconceptions regarding blacks that his society has ingrained in him, and, as already mentioned, at the end of the novel, he is quick to become a subordinate to Tom Sawyer. Yet for all of these imperfections, and perhaps because of them, we recognise in Huck a falliable, human boy, who nevertheless shows himself through the course of the novel to be a thinking, feeling human being rather than just a product of society.