I had a small kitchen fire and the electrical wire behind the drywall in my kitchen melted. What is the best way to safely and legally (to code) rejoin the wire?
The "best way" is to replace all the damaged wire.
Either completely (from where it starts and ends now) or by adding two junction boxes (which must remain accessible) where you can join the undamaged parts of the wire to new undamaged wire between the two junction boxes. If you can reach one end with new wire, you may only need one new junction box.
All splices (except for the Tyco splice mentioned in kg333's answer) must be made inside junction boxes.
The whole junction box cover must remain accessible forever without needing tools to disassemble the building in any way (but unlike a subpanel it does not require 'working space maintained 24x7).
The undamaged cable must enter the junction box via a strain relief or grommet that is UL-listed for that cable type and size.
The sheath must extend at least 1/4" into the junction box (bonus points for a bit more and using that to mark where it goes)
The separated individual wires must have at least 6" of length inside the box
The wires must extend at least 3" beyond the finished wall surface, unless the box is big enough to work with both hands (e.g. a 10x10 box does not require this rule).
The number of wires in the box cannot exceed the "box fill limits" (vague rule of thumb: 2.25 cubic inches per wire, but grounds are "on sale 4 grounds for the price of 1).
The box CAN have receptacles or switches on its cover... however those themselves require a box fill allocation (2x the wire size attached to it)
Pigtails are free.
Presuming the cable in your wall is non-metallic (NM), you can use an in-wall rated NM splice. Here's a datasheet of such a splice as an example (not a recommendation of this product in particular)
Keep in mind that such a device has to be rated for in-wall use, appropriately sized for the cable you use it on, and has to be allowed by local code: you can't just use wire-nuts or crimp connectors that aren't rated for in-wall, and some jurisdictions don't allow this type of splice.
Unless you definitely know that the cause of the "small kitchen fire" was not electrical you shouldn't be splicing any wires.
A proper electrical survey (at least for the relevant circuit) should be carried out first.
If you do just splice the two wires together, at least include a MCB in the circuit - even a fused socket is better than nothing!